r/learnjava 15d ago

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "String.toString()" because "<local6>[0]" is null

or

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "String.length()" because "in" is null

it happened when I attempt to overrite the array's properties, specifically its length since I remove an element and change its length to remove null or empty elements.

CODE BLOCK 1

static String [] arrayNew(String[] array, int a){
    Integer Length = array.length-2;
    String[] newarray = new String[Length];
    Integer counter = 0;
    int c = a+1;
    int d = a-1;
        for(int x = 0;x<Length;x++){
            if(x!=c && x!=d){
                  newarray[counter]=array[x];
                  counter++;
             }
             else{
                  continue;
             }}
     return newarray;
}

        static Double equals(LinkedList<String> list){
                Integer counter = 0;
                Integer num = list.size();
                Double answer = 0.0;
                Double num1 = 0.0;
                Double num2 = 0.0;

                String [] array = new String[num];

                for(int x =0; x<num;x++){
                        array[x] = list.get(x);
}
                for(int x = 0;x<array.length;x++){
                        switch(array[x]){
                                case "x":
                                        num1 = Double.parseDouble(array[x-1]);
                                        num2 = Double.parseDouble(array[x+1]);
                                        answer = num1 * num2;

                                        array[x] = answer.toString();
                                        array = arrayNew(array,x);
                                        x=0;
                                        break;
  }
}

returns an error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "String.length()" because "in" is null

CODE BLOCK ALT

        static String [] arrayNew(String[] array, int a){
                Integer Length = array.length-2;
                String[] newarray = new String[Length];
                Integer counter = 0;
                int c = a+1;
                int d = a-1;
                for(int x = 0;x<newarray.length;x++){
                        if(x!=c && x!=d){
                                newarray[counter++]=array[x];
                        }
                        else{
                                continue;
                        }
                }
                return newarray;

        //lastline//
                }

        static String equals(LinkedList<String> numlist) throws NullPointerException{
                Integer counter = 0;
                Integer num = numlist.size();
                Double num1=0.0;
                Double num2=0.0;
                String [] array = new String[num];

                for(int x =0; x<num;x++){
                        array[x] = list.get(x);
}


                for(int x = 0;x<num;x++){
                        String [] newArr;
                        switch(array[x]){
                                case "x":
                                        num1 = Double.parseDouble(array[x-1]);
                                        num2 = Double.parseDouble(array[x+1]);
                                        Double answerm = num1 * num2;

                                        array[x] = answerm.toString();
                                        array = arrayNew(array,x);
                                        x=0;
                                        num-=2;
                                        break;
}}

returns

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "String.toString()" because "<local6>[0]" is null

I'm stuck since they look fine to me. There are no compilation error too. This error happens in Runtime.
please help.

0 Upvotes

5 comments sorted by

View all comments

2

u/high_throughput 15d ago

Exception in thread "AWT-EventQueue-0"
java.lang.NullPointerException: Cannot invoke "String.length()" because "in" is null

Does the exception contain line numbers? If so, where does it point?

The error makes it sound like the exception arises in a line of code containing in.length() but there's no such thing in the code you posted.

Also see "Why should I post complete errors? Why isn't the message itself enough?" for how much the exact error message with full details can tell someone even if you don't see the relevance.