【问题标题】:logResponse Exception in thread "main" java.lang.NullPointerException线程“主”java.lang.NullPointerException 中的 logResponse 异常
【发布时间】:2013-08-25 07:44:12
【问题描述】:

我已经搜索并发现了一些似乎没有帮助的响应,并且我遇到了 nullPointerException 错误。下面是我的代码,错误出现在我的 logResponse() 方法中,非常感谢任何帮助。

import java.util.*;

public class Survey21 {

    private Scanner in = new Scanner(System.in);
    private String surveyTitle;
    private static int rID;
    private static int respondentID;
    private int[][] responses;

    //Constructors
    // Default Constructor
    Survey21() {
        surveyTitle = "Customer Survey";
    }

    // Overloaded Constructor 1
    Survey21(String title, int rID) {
        surveyTitle = title;
        rID = 0;
        generateRespondentId();
    }

    Survey21(int[][] surveyArray) {
        responses = surveyArray; // store responses
    }

    public static int getrID() {
        return rID;
    }

    public static void setRespondentID(int respondentID) {
        respondentID = rID;

    }

    public String getTitle() {
        return surveyTitle;
    }

    public void setTitle(String title) {
        surveyTitle = title;
    }

    public static int generateRespondentId() {
        rID = ++respondentID;
        return rID;
    }

    // displays name of survey and entire grid of results
    public void displaySurveyResults() {
        System.out.printf("%s\n\n", getTitle());
        logResponse();


    }

    // dispalys question number and results for that question so far
    public void displayQuestionStats(int questionNumber) {
    }

    // enter questions and store in an array of Strings
    public void enterQuestions() {
        /*String[] questions = {
         "How would you rate your online shopping experience?",
         "How satisfied was you with the purchase price?",
         "Overall how was the online checkout experience?",
         "How likely are you to recommend your friends and family to our store?",
         "How concerned are you with online credit card security?",
         "How likely are you to prefer a retail location compared to an online store?",
         };*/
        String questions[] = new String[10];

        for (int i = 0; i < questions.length; i++) {
            System.out.println("Please enter a question!");
            questions[i] = in.nextLine();
        }
        /*TEST TEST***
         System.out.print(questions[0] + "\n");
         System.out.print(questions[1] + "\n");
         System.out.print(questions[2] + "\n");
         System.out.print(questions[3] + "\n");
         System.out.print(questions[4] + "\n");
         System.out.print(questions[5] + "\n");
         System.out.print(questions[6] + "\n");
         System.out.print(questions[7] + "\n");
         System.out.print(questions[8] + "\n");
         System.out.print(questions[9] + "\n");
         */

    }

    **// enters the response in the correct grid 
    public void logResponse() {
        System.out.println("The responses are:\n");
        System.out.print("            "); // align column heads
        // create a column heading for each question
        for (int qNumber = 0; qNumber < responses[0].length; qNumber++) {
            System.out.printf("Question number %d ", qNumber + 1);
            System.out.println("Response"); // column heading 
        }
        for (int response = 0; response < responses.length; response++) {
            System.out.printf("Response %2d", response + 1);
            for (int qNumber : responses[response])// output responses
            {
                System.out.printf("%8d", qNumber);
            }
        }
    }**
}

【问题讨论】:

    标签: java multithreading exception main


    【解决方案1】:

    我没有正确初始化我的数组

    private int[][] responses;
    

    应该是这样的

    private int[][] responses = new int[5][11];
    

    【讨论】:

      【解决方案2】:

      您可能想要数组的长度而不是第一个元素的长度:

      for (int qNumber = 0; qNumber < responses.length; qNumber++) {
          System.out.printf("Question number %d ", qNumber + 1);
          System.out.println("Response"); // column heading 
      }
      

      【讨论】:

      • 客户调查响应是:SurveyTest21 的 Survey21.displaySurveyResults(Survey21.java:62) 的 Survey21.logResponse(Survey21.java:108) 的线程“main”java.lang.NullPointerException 中的异常。 main(SurveyTest21.java:16) Java 结果:1
      • 您的响应数组似乎为空。您没有在每个构造函数中设置响应数组。你用的是哪个构造函数?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2014-01-12
      相关资源
      最近更新 更多