是什么导致线程"pool-1-thread-2"中的异常? (空指针异常) [英] What is causing the Exception in thread "pool-1-thread-2" (NullPointerException)

查看:215
本文介绍了是什么导致线程"pool-1-thread-2"中的异常? (空指针异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个多线程的Web服务器.当我运行客户端时,我收到一个NullPointException.为什么会发生这种情况,该怎么办才能解决呢?

关于我应该阅读上述教程的建议.我有 试图借助它来解决问题.我创建了一个 构造函数,但后来我不知道如何调用该类: theExecutor.execute(new Broadcaster (connection);

我还创建了一个setter:public void setQuestions(String[] questions) { this.questions[1] = questions[1];}

那也没有帮助.字符串在同一位置初始化 作为questions[0].我可以在控制台中打印questions[1].它 从ArrayList获取它的值,但是我不知道如何传递它 HandleValidation类的值.这似乎是问题所在. 如果我错过了本教程中明显的解决方案,请告诉我!

在这里您可以阅读异常消息和堆栈跟踪:

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

在这里,我正在初始化正在处理线程的Executor:

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));
        }

字符串问题[1]的起源在Broadcaster类中:

我正在使用get和set方法,以使另一个内部类可以使用

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;
    }

以下是创建的字符串questions[0]questions[1]:

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();
                        }

NullPointerException发生在HandleValidation类中:

class HandlleValidation extends Thread {

问题是由以下行引起的:

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

二传手:

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

我试图尽量减少描述的代码 问题.如果需要,我也许可以提供更多信息!

解决方案

我在for (String string: questionsList) {的上下文中看到了该变量,并且又在您在if (answer.equals (questions [1])) {中使用它了:即在其他上下文中,在其他范围内. /p>

我的建议是将该变量声明为 for 范围之外.

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!

解决方案

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.

这篇关于是什么导致线程"pool-1-thread-2"中的异常? (空指针异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆