Welcome to Dream.In.Code
Become a Java Expert!

Join 150,365 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,795 people online right now. Registration is fast and FREE... Join Now!




String.CompareTo exception error

 
Reply to this topicStart new topic

String.CompareTo exception error

wingz198
24 Jan, 2008 - 02:28 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2005
Posts: 55


My Contributions
I have the code below to compare two strings and when I run it, the console shows a Null Pointer Exception. I've tried using a variable for the second string and a literal string and the same thing comes up.

//pcData is a string
if (pcData.compareTo(err) == 0){....}

User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: String.CompareTo Exception Error
24 Jan, 2008 - 02:34 PM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
probably your err string is blank. heres an example
CODE

class aa    {
    public static void main(String args[])    {
        String a = "a";
        String b = "a";
        if(a.compareTo(b) == 0)
            System.out.println("strings a and b are the same");
        }
    }


This post has been edited by dontKnowJava: 24 Jan, 2008 - 02:36 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: String.CompareTo Exception Error
24 Jan, 2008 - 02:34 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
maybe you are comparing a string with null, can the function .compareTo() compare String object with null?
Check the API.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: String.CompareTo Exception Error
24 Jan, 2008 - 02:45 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Chances are, pcData==null. It doesn't matter if err is null or not.
User is online!Profile CardPM
+Quote Post

dontKnowJava
RE: String.CompareTo Exception Error
24 Jan, 2008 - 03:08 PM
Post #5

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
QUOTE(baavgai @ 24 Jan, 2008 - 03:45 PM) *

Chances are, pcData==null. It doesn't matter if err is null or not.

i think it does. gave me same null pointer error on either being null
User is offlineProfile CardPM
+Quote Post

baavgai
RE: String.CompareTo Exception Error
24 Jan, 2008 - 04:10 PM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(dontKnowJava @ 24 Jan, 2008 - 06:08 PM) *

QUOTE(baavgai @ 24 Jan, 2008 - 03:45 PM) *

Chances are, pcData==null. It doesn't matter if err is null or not.

i think it does. gave me same null pointer error on either being null



Oops, you are correct. Damn, Java just loves it's Exceptions. tongue.gif

I was thinking of C# when I posted, actually. I'll have to test that out in C# when I'm back on a Windows box.

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month