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 outquestions[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]andquestions[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!
 
     
     
    