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

Thread: java.lang.NullPointerException

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default java.lang.NullPointerException

    I have typed this code exactly the same way on my last program and now I am getting the following error!

    Exception in thread "main" java.lang.NullPointerException
    at Survey.enterQuestions(Survey.java:21)
    at SurveyTest24.main(SurveyTest24.java:13)
    Java Result: 1

    Here is my code:
     
    import java.util.*;
     
    public class Survey {
     
        Scanner in = new Scanner(System.in);
        private String surveyTitle;
        private static int CurrentID = 0; // holds the current respondent Id
        private int[][] Responses;
        private String[] Questions;
        public final static int MAX_RESPONSES = 10;
        public final static int MAX_QUESTIONS = 10;
        public final static int PRINT_ALL = -1;
     
        Survey(String title) {
            surveyTitle = title;
            // displaySurveyResults();
        }
     
        public void enterQuestions() {
            for (int i = 0; i < Questions.length; i++) {
                System.out.println("Please enter a question! ");
                Questions[i] = in.nextLine();
            }
        }
     
        public final void displaySurveyResults() {
            System.out.printf(surveyTitle);
        }
     
        // Print bar chart for each question
        private void displayQuestionStats(int qNumber){
     
        }
     
        private void logResponse(int ID, int qNumber, int response){
     
        }
    }

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: java.lang.NullPointerException

    Show the class with the main() method that uses this class.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException

    /*
     * Author: Thomas Stratmann
     * Course: IT152
     * Date: Sep 2, 2013
     * Program: IT152
     * Phase 1 Individual Project, Customer SurveyOld
     */
     
    public class SurveyTest24 {
     
        public static void main(String[] args) {
            Survey survey = new Survey("Customer Survey");
            survey.enterQuestions();
            survey.displaySurveyResults();
        }
     
     
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: java.lang.NullPointerException

    So when I run your code with the main() method tucked into the Scanner class, I get:
    Exception in thread "main" java.lang.NullPointerException
    	at Survey.enterQuestions(Survey.java:22)
    	at Survey.main(Survey.java:48)
    Note that the complaint is caused by the call to method enterQuestions() and the line specifically to the NPE is:

    for (int i = 0; i < Questions.length; i++)

    What in that line could be null? Well, it's fairly obvious that it can only be Questions.length. Why is that? Well, Questions is declared as an instance variable, but it is never initialized. It's a container into which nothing has been placed.

    I doubt this code is exactly the same as you had before. Check the Survey() constructor and see if there's code missing there. If not there, then the code is missing from the enterQuestions() method. No matter where it's missing, the Questions[] array must be initialized to be used.

    Also, respect Java naming conventions: names of variables and methods should begin with lower case letters.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by GregBrannon View Post
    Also, respect Java naming conventions: names of variables and methods should begin with lower case letters.
    That is what I thought the variables is exactly what my instructor told me to use.

    Also you are correct originally I used String questions[] = new String[10], which seemed to work but as I said the variables is what the instructor told me to use so I was trying to use them!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: java.lang.NullPointerException

    Then do as your instructor says. That's where your grade comes from. It's not a "hill to die on," and better to pass than to be right.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by GregBrannon View Post
    What in that line could be null? Well, it's fairly obvious that it can only be Questions.length.
    <nitpick>
    Since length is a primitive it cannot be null. Obviously Questions is null.
    </nitpick>
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    GregBrannon (September 3rd, 2013)

Similar Threads

  1. java.lang.NullPointerException
    By tangara in forum Exceptions
    Replies: 14
    Last Post: August 12th, 2013, 06:34 AM
  2. java.lang.NullPointerException
    By nikomania in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2013, 04:16 PM
  3. [SOLVED] java.lang.NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 10:39 AM
  4. java.lang.NullPointerException
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2011, 04:39 PM
  5. java.lang.nullPointerException
    By ridg18 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2010, 03:52 PM