0

I was doing an assignment for a Computer Science course I'm taking and I tried to declare 3 String arrays in one line but it kept giving me this error message

java.lang.NullPointerException

I messed around with it a bit and I fixed the problem by changing

public static String[] offence,name,date = new String[8];

to

public static String[] offence,name = new String[8];

public static String[] date = new String[8];

Why does it only work when I do this?

PS, I'm new so go easy on me.

    package pDatabaseApp;

    import java.io.*;

    public class PDatabaseMenu {

    public static String[] offence,name = new String[8];
    public static String[] date = new String[8];

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

        String line;

        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));

        boolean finnish = false, reenter = false;

        while(finnish == false) {

            int i;

            for(i = 0; i <= 7; i++) {

                System.out.println("enter a name");

                name[i] = in.readLine();

                System.out.println("enter a day");

                String day = in.readLine();

                System.out.println("enter a month");

                String month = in.readLine();

                System.out.println("enter a year");

                String year = in.readLine();

                date[i] = day + "/" + month + "/" + year;

                String offenceEnter[] = new String[3];
                String offenceType[] = {"Assault","Arson","Theft"};

                int l;
                for(l = 0; l <= 2; l++) {

                    System.out.println("is there offence " + offenceType[l]);
                    offenceEnter[l] = in.readLine();

                    if(offenceEnter[l] == "yes") {

                        offence[i] = offenceType[l];

                    }
                }

            }

        }

        in.close();
    }
}
Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • ` = new String[8]` only applies to the variable immediately preceding it, not all the variables being declared on that line. So, the other variables are remaining uninitialized. – GriffeyDog Aug 16 '18 at 18:19
  • Also, dont compare Strings with ==. https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – GriffeyDog Aug 16 '18 at 18:21

5 Answers5

2

Remember the code,

public static String[] offence,name,date = new String[8];

will only initialize date. So that, the other variables are remaining uninitialized. As a result you have got java.lang.NullPointerException.

You'd need something like

 public static String[] offence= new String[8],name= new String[8],date = new String[8];
Yug Singh
  • 3,112
  • 5
  • 27
  • 52
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
1

The way you have the code written above, the array offence ends up being null. If you want to initialize all three variables you should have this.

public static String[] offence = new String[8],name = new String[8],date = new String[8];
0

Using the comma between offence and name, like so,

public static String[] offence,name = new String[8];

is a shortcut for:

public static String[] offence;
public static String[] name = new String[8];

This question is very closely related: Initializing multiple variables to the same value in Java

Jamie
  • 1,888
  • 1
  • 18
  • 21
0
public static String[] offence,name,date = new String[8];

The code above implies that you would be trying to declare and initialize one String array of size 8 with three pointers to it: offence, name, and date. For mutable objects, like arrays, it usually makes more sense to initialize each object separately.

That being said, I'm not sure why your amended code works, but I figure it won't behave the way you want it to. I would suggest doing the following:

public static String[] offence, name, date;
offence = new String[8];
name = new String[8];
date = new String[8];

Hopefully I was able to help!

Victor
  • 48
  • 1
  • 4
0
package pDatabaseApp;

import java.io.*;

public class PDatabaseMenu {

public static String[] offence = new String[8];
public static String[] name = new String[8];
public static String[] date = new String[8];

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

    String line;

    BufferedReader in;
    in = new BufferedReader(new InputStreamReader(System.in));

    boolean finnish = false, reenter = false;

    while(finnish == false) {

        int i;

        for(i = 0; i < 8; i++) {

            System.out.println("enter a name");

            name[i] = in.readLine();

            System.out.println("enter a day");

            String day = in.readLine();

            System.out.println("enter a month");

            String month = in.readLine();

            System.out.println("enter a year");

            String year = in.readLine();

            date[i] = day + "/" + month + "/" + year;

            String offenceEnter[] = new String[3];
            String offenceType[] = {"Assault","Arson","Theft"};

            int l;
            for(l = 0; l < 3; l++) {

                System.out.println("is there offence " + offenceType[l]);
                offenceEnter[l] = in.readLine();

                if(offenceEnter[l].Equals("yes"))//A for each is better but idk if you did that

{

                    offence[i] = offenceType[l];

                }
            }

        }

    }

    in.close();
}
}
A_Arnold
  • 3,195
  • 25
  • 39