Results 1 to 3 of 3
  1. #1
    waterisgood5 is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Need Help with NullPointerException Error

    Hi, Im new to programming and im trying to write a simple quiz in java. I was just testing to see if i can get my first question from in, but i keep getting a NullPointerException Error. Any ideas on how to fix it?

    //////this is
    PHP Code:
    import java.util.*;
    public class Question
    	private String[] questionArr;
    	private String[] ansArr;
    	private String[] statArr;
    	private String[] correctAns = new String[10];
    	private	String[] questionCorrect = new String[10];
    	private String[] questionTried = new String[10];
    	private int[] numAnsArr = new int[10];
    	private File file;
    	private int numAns;
    	public Question(File f)
    		file = f;
    	public void GetData() throws FileNotFoundException
    		Scanner inFile = new Scanner(file);
    		String numQ = inFile.nextLine();
    		int numQuestions = Integer.parseInt(numQ);
    		String[] questionArr;
    		String[] ansArr;
    		String[] statArr;
    		questionArr = new String[numQuestions];
    		for(int i=0;i<numQuestions;i++)
    			questionArr[i] = inFile.nextLine();
    			String numA = inFile.nextLine();
    			numAns = Integer.parseInt(numA);
    			numAnsArr[i] = numAns;
    			ansArr = new String[numAns];
    			for(int a=0;a<numAns;a++)
    				ansArr[a] = inFile.nextLine();
    			correctAns[i] = inFile.nextLine();
    			questionCorrect[i] = inFile.nextLine();
    			questionTried[i] = inFile.nextLine();
    	public String[] getQuestion()
    		return (questionArr);
    	public String[] getAnswers()
    		return (ansArr);

    ////This is
    PHP Code:
    import java.util.*;
    public class Main
    	public static void main(String[] args) throws FileNotFoundException
    		File file = new File("questions.txt");
    		Question cl = new Question(file);
    		String[] questionA;
    		questionA = cl.getQuestion();
    Any ideas?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    Quote Originally Posted by waterisgood5 View Post
    Any ideas?
    Method GetData() initializes your Question object but it is never called. Your Question object is a very fragile object, i.e. after you've made one it is virtually useless, you have to call that GetData() method. Shouldn't it be called when the object is instantiated? Another question: what happens if the GetData() method fails? You don't want a useless Question object hanging in there ...

    kind regards,


  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    Just a general point on runtime error messages if you can't understand them: the standard procedure here is to copy and post the entire stack trace along with an indication of which line of your code it is referring to. (the last bit so that folk don't have to count to line 42 or whatever).

    I haven't read your code (trusting in Jos to have put you on the right track) but the general procedure I use for those pesky NullPointerExceptions is to

    * locate which line of my code appears first in the stack trace
    * then try and figure out what on that line was null. (System.out.println() is my friend)
    * finally look back to where I thought that variable was given a nonnull value and figure out why that didn't happen

    This doesn't always spot the error as the NPE can occur for other reasons, but it is productive quite often.

Similar Threads

  1. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 02:31 PM
  2. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  3. awt TextField nullPointerException error
    By k2k in forum AWT / Swing
    Replies: 3
    Last Post: 02-24-2009, 04:24 AM
  4. ERROR: nullPointerException in applet
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:11 AM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts