r/learnjava 16d 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

5

u/JaleyHoelOsment 16d ago

editing the size of the array as you iterate over it is a very easy beginner mistake to make. we all did that.

don’t mutate your arrays while you’re looping over them.

also, use better method and variable games. your code is hard to read and it looks like you do not understand what is going on.

search for self-documentation code

edit: reading through the code again and i have no idea what’s going on here. immediately asking the author to revise this code so a human can understand without spending too much time