-1

I have a NullPointerException, I have debugged the code and was able to trace the problem but how do I solve it?

Minimal test case:

    ExecutorService theExecutor = Executors.newFixedThreadPool(10000); //Skapar nya Threads, samt begränsar antalet.
    ServerSocket quizSocket = new ServerSocket(serverPort);

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

Broadcaster class. where the problem occurs:

 public static class Broadcaster extends Thread {

    String quizFile = "src/qa.txt";  // Format of text: Vad heter äventyrets hjälte?/Frodo
    private Socket connection;
    private PrintStream write;
    private String qString;
    private String answer;
    private String question;   // (Debug message) question: null
    int points = 0;

    public Broadcaster(Socket connection) {
        this.connection = connection;
    }

    @Override
    public void run() {
        try {
            write = new PrintStream(connection.getOutputStream());
            //Skriva till klienten.
            List<String> questionsList = new ArrayList<>();

            try (Stream<String> questionsStream = Files.lines(Paths.get(quizFile))) {  //Reading from text file
                questionsList = questionsStream
                        .parallel()
                        .collect(Collectors.toList());
                Collections.shuffle(questionsList);  //Randomizing the Strings.

              while (true) {

    for (String qString : questionsList) {
    String[] questions = qString.split("/");  //Splitting to question[0] and [1]
    write.println(questions[0]);  //Printing out [0]. Has a valid value

    question = questions[1].toLowerCase(); //question = null. question[1] "Sam"

The string question is still null, even though question[1] is not. The NullPointerException is a fact! Which means that this variable sets a null value to the getter later on:

   public void setQuestion(String question) {
   this.question = question;

I want to declare a variable, with the value of question[1].toLowerCasewithout causing a NullPointerException. Then I want to generate a setter with this variable. But how do I do this? In a previous post, I received a duplicate warning and was suggested to follow this tutorial. Now I have done that, and this is what I came up with. Still need advice how to solve the actual problem! Where do I go from here?

For more information, visit this post!

Community
  • 1
  • 1
Josef Lundström
  • 197
  • 2
  • 15
  • 3
    `toLowerCase` never returns `null` - something else must be up. Please construct a [minimal test-case](http://stackoverflow.com/help/mcve). – Oliver Charlesworth Apr 16 '17 at 14:25
  • 1
    Thank you for your effort but it is not a reproducible minimal example because if `questions[1]` is not null, this instruction : `question = questions[1].toLowerCase();` cannot assign a `null` value to `question`. Please try to be more clear in the presentation of your problem. – davidxxx Apr 16 '17 at 14:30
  • 1
    You don't have a `Nullpointerexception`. There is no such thing. Perhaps you have a `NullPointerException`. In java, character case matters. – Mike Nakis Apr 16 '17 at 14:40
  • 1
    Kudos for debugging your program, thus at least making an effort to trace the problem before bringing it to us, but I am afraid you have not found the problem. The problem is not what you think it is. So, read what @OliverCharlesworth wrote. – Mike Nakis Apr 16 '17 at 14:43
  • 1
    You're missing the stacktrace in your question, but all nullpointerexceptions happen for the same reason – OneCricketeer Apr 16 '17 at 15:01
  • Is `questions[1].toLowerCase()` raising a `NullPointerException`? – G. Fiedler Apr 16 '17 at 17:53
  • questions[1] raises a NullPointerExcepton. I have debugged. But there may be something else that I am not aware of. Quite new to Java. – Josef Lundström Apr 16 '17 at 20:06
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems), you are using the step-debugger provided with your development environment are you not? –  Apr 18 '17 at 13:39

1 Answers1

0

toLowerCase() requires localization information because upper-case to lower-case transformation is locale specific. The parameterlesstoLowerCase() converts to lower case using the rules of the default locale. When there is a problem with the default locale a NullPointerException may be raised.

Use an explicit locale parameter to solve the problem: toLowerCase(Locale.ENGLISH)

G. Fiedler
  • 664
  • 4
  • 12