1

I am a bit lost...I'm learning Java and have to program a small poll command line application.

We are supposed to program it in german first(to be consistent between us all), so I'll try to translate it, so it's easier to read for you. My problem is, that it's throwing an exception (while compiling) as following:

Exception in thread "main" java.lang.NullPointerException

at communication.Poll.addQuestionItem(Poll.java:18)

at main.PollTool.main(PollTool.java:8)

am I initializing my array "questionItems" wrong? Aren't I supposed to do it like that? What's wrong here? Did I forget something? :(

main.PollTool:

package main;
import communication.Poll;

public class PollTool {

    public static void main(String[] args) {
        Poll poll = new Poll ("Best Smartphone:",3);
        poll.addQuestionItem("iPhone");  //<--line 8
        poll.addQuestionItem("Android");
        poll.addQuestionItem("Windows Phone");

        poll.askQuestions("This poll determines the polularity of different Smartphones.");

    }

}

communication.Poll:

package communication;
import java.util.Scanner;
import calculations.QuestionItem;

public class Poll {
    private String questionTitle;
    private QuestionItem[] questionItems;
    private int count;
    private Scanner in = new Scanner(System.in);

    public Poll(String s,int arraySize){
        questionTitle = s;
        questionItems = new QuestionItem[arraySize]; //<--problem here?
    }

    public void addQuestionItem(String s){
        if(count<questionItems.length){
            questionItems[count++].setItemText(s); // <--exception here
        }
    }

    public void askQuestions(String topic){
        System.out.println(topic);

        System.out.println(questionTitle);
        for(int i=0; i<questionItems.length; i++){
            System.out.println("- - - "+ questionItems[i].getItemText() +" - - -");
            System.out.print("Your numerical answer: ");
            questionItems[i].vote(in.nextInt());
        }
    }

    void evaluation(){
        //not ready :)
    }
}

calculation.QuestionItem:

package calculation;

public class QuestionItem {
    int count; 
    int overall;
    String text;

    public void vote (int pointValue){
        overall += pointValue;
        count++;
    }
    public double getDurchschnitt(){
        return (double) overall/count;
    }
    public void setItemText(String s){
        text = s;
    }
    public String getItemText(){
        return text;
    }
}
Rasalas
  • 1,552
  • 2
  • 12
  • 15
  • 1
    This question was not duplicate. Just because he gets NullPointerException error doesn't mean it's the same with other NullPointerException questions... – omerfarukdogan Apr 26 '15 at 17:58

2 Answers2

6

By the looks of it, you're making the array but it doesn't contain the objects yet. You probably want this in the constructor instead.

questionItems = new QuestionItem[arraySize];
for(int i = 0; i < questionItems.length; i++) {
    questionItems[i] = new QuestionItem();
}
Constant
  • 780
  • 1
  • 5
  • 19
6

When you initialize an array of objects like this:

questionItems = new QuestionItem[arraySize];

All of the values are null by default.

In addQuestionItem, you try to call a method on an object in the array. However, that object starts off null, so this line of code doesn't work:

questionItems[count++].setItemText(s);

What you have to do is initialize the object before setting the text:

questionItems[count] = new QuestionItem();
questionItems[count].setItemText(s);
count++;

Alternatively, you can do what Constant suggested, and initialize all the objects when you initialize the array.

Community
  • 1
  • 1
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • 1
    thank you - I didn't know that I have to initialize them "again". But you decribed it pretty well, so that it made sense :) – Rasalas Apr 26 '15 at 18:13