What is causing the Exception in thread “pool-1-thread-2” (NullPointerException) [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-01-25 22:18:09

问题


I am creating a multi-threaded web server. When I run the client I receive a NullPointException. Why is this happening and what can be done to solve it?

Concerning the suggestions that I should read the mentioned tutorial. I have tried to solve the problem with the help of it. I created a constructor, but then I didn't know how to call the class: theExecutor.execute(new Broadcaster (connection);

I also created a setter: public void setQuestions(String[] questions) { this.questions[1] = questions[1];}

That didn't help either. The String is initialized at the same place as questions[0]. I can print out questions[1] in the console. It get's its value from the ArrayList, but I do not know how to pass that value to the HandleValidation class. That seems to be the problem. If I have missed something in the tutorial that is an obvious solution, please tell me!

Here you can read exception message and the stack trace:

Exception in thread "pool-1-thread-2" java.lang.NullPointerException
at HandlleValidation.run(QuizServer.java:121)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745

Here I am initializing the Executor which are handling the threads:

public class QuizServer {
public static void main(String[] args) throws IOException {

ExecutorService theExecutor = Executors.newFixedThreadPool(10000);
    ServerSocket quizSocket = new ServerSocket(serverPort);

    try {
        while (true) {  //Skriver ut While True
            Socket connection = quizSocket.accept();
            theExecutor.execute(new Broadcaster(connection));
            theExecutor.execute(new HandlleValidation(connection));
        }

The origin of the String questions[1] is in the Broadcaster class:

I am using a get and set method to make it possible for the other inner class to work with

String questions[];

static class Broadcaster extends Thread {

    String quizFile = "src/qa.txt";
    private Socket connection;

    private PrintStream write;
    private String answer;
    private String questions[];
    int points = 0;

    public String[] getQuestions() {
        return questions;
    }

Here are the Strings questions[0] and questions[1]created:

while (true) {

                    for (String string : questionsList) {
                        String[] questions = string.split("/");
                        write.println(questions[0]);  //Printing out in client.
                        try {
                            Thread.sleep(4000); //"Sleeping" between the lines.
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

The NullPointerException occurs in the HandleValidation class:

class HandlleValidation extends Thread {

The problem is caused by this line:

  if (answer.equals(questions[1])) {
                points++;
                writer.println("RÄTT SVAR! Du har " + points + " poäng");

The Setter:

public void setQuestions(String[] questions) {
    this.questions = questions;

I have tried to include as little of the code as possible to describe the problem. I may be able to give more information if requested!


回答1:


I see the variable in context of for (String string: questionsList) { and in another moment you use it in if (answer.equals (questions [1])) {: that is in other context, in other scope.

My suggestion is to declare that variable out of the for scope.



来源:https://stackoverflow.com/questions/43400548/what-is-causing-the-exception-in-thread-pool-1-thread-2-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!