Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: String to Int messing up

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default String to Int messing up

    Hi, I have a problem where when I try to convert a string to an int, an error comes up. I'm converting the string from a text file so that probably has something to do with it. The text file has nothing on it, so I would think it would read, ""(nothing). Here is the relevant code:

    static File file = new File("temp");
    static String dir = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4 );
    File rfile3 = new File(dir + "linenum.txt");
    ...
                           FileInputStream fis4 = null;
    	                BufferedInputStream bis4 = null;
    	                DataInputStream dis4 = null;
    ...
    	                fis4 = new FileInputStream(rfile3);
    	                  bis4 = new BufferedInputStream(fis4);
    	                  dis4 = new DataInputStream(bis4);
    	                  String rlinenumRead = dis4.readLine();
    	                  System.out.println(rlinenumRead); //This line yields "null"
    	                  if(rlinenumRead.equals(null)||rlinenumRead.equals("")||rlinenumRead.equals("null")) //**
    	                  {
    	               	        rlinenumRead.equals("0");
    	                  }
    	                  int rlinenumInt = Integer.parseInt(rlinenumRead);

    **=I'm trying multiple possibilities of what it could read as.

    Here is the error I'm getting:

     

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at layout.testing2.actionPerformed(testing2.java:337) //This is the line I starred above
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)




  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String to Int messing up

    Note the readLine int DataInputStream is deprecated (BufferedReader is recommended). Also note that the readLine methods in the readers will return null if the end of the stream is reached.

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: String to Int messing up

    If it returns null, then why wouldn't
    if(rlinenumRead.equals(null))
    work? Is that not how I'm suppose to say "if the string returns null"?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String to Int messing up

    Quote Originally Posted by The_Mexican View Post
    If it returns null, then why wouldn't
    if(rlinenumRead.equals(null))
    work? Is that not how I'm suppose to say "if the string returns null"?
    Because rlinenumRead is not an object, it is null, so by definition a NullPointerException will be thrown. Try using
    if ( rlinenumRead == null )
    to check for a null value.

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: String to Int messing up

    Ok you're right, and I also had to change
    rlinenumRead.equals("0");
    to
    rlinenumRead = ("0");
    Strange...I remember being taught that with strings, you use .equals instead of equal signs...

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String to Int messing up

    You should use equals, so don't change to ==. The exception (pun intended) is if there is a possibility that the string you wish to call equals on could be null, in which case you first check for null, then check equals
    String s1 = "test";
    Stirng s2 = null;
    if ( s1 != null ){
        if ( s1.equals("test")){
            //do something
        }
    }
    if ( s2 != null ){
        if ( s2.equals("test") ){
     
        }
    }
    Last edited by copeg; November 28th, 2010 at 04:14 PM.

  7. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (November 28th, 2010)

Similar Threads

  1. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  2. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 11:16 AM
  3. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM