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 4 of 4

Thread: NullPointerException but dont know why

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException but dont know why

    When I run class Programmalijst, I get a NullpointerException.
    It states that my x.hasNext() is null, I dont know why. Im a beginner in Java.


    import java.util.*;
    import java.io.File;
     
     
    public class Datatype {
     
    	public Scanner x;
     
    	public void openFile (){
    		try{
    			x = new Scanner (new File("programmas.txt"));
     
    		}
    		catch(Exception e){
    			System.out.println("error could not find file");
    		}
    	}
     
    	public void getZender(){
    		while(x.hasNext()) {
    			String zender = x.next();
    			System.out.println(zender);
    		}
     
    	}
    public void closeFile (){
    		x.close(); 
    	}
     
    }

    public class Programmalijst {
     
    	public static void main (String [] args){
     
    		Datatype r = new Datatype ();
     
    		r.getZender();
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default Re: NullPointerException but dont know why

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Posting the entire error message and stack trace is helpful, because it includes important information.

    The variable 'x' is initialized inside the method openFile(). If that is not done before the method getZender() is called, it will be null or uninitialized when called in getZender(). The solution is to ensure it has been initialized using a 'new' statement before it is used anywhere in the program. Those kind of initializations are often done in a constructor but can be done anywhere as long as it gets done.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException but dont know why

    Thank you, however I put r.openFile() in it, but I still get the same exception.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,657
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: NullPointerException but dont know why

    Can you provide updated code? Did you call r.openFile() BEFORE getZender()?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. i dont know where the problem is...help me here
    By Shoes Mosh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 6th, 2014, 11:27 PM
  2. [SOLVED] I have a NullPointerException error that I dont understand
    By LukeR in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2013, 05:15 PM
  3. Dont understand the nullpointerexception
    By xxxDanxxx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 9th, 2013, 06:09 AM
  4. i'm getting this error, dont know why? please help
    By amr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 17th, 2010, 05:14 AM
  5. I dont get it....please help
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 30th, 2010, 03:16 AM