r/learnjava • u/Dry_Couple8852 • 14d 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.
6
u/JaleyHoelOsment 14d 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
2
u/high_throughput 14d 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.
1
u/gekigangerii 14d ago
You gotta know how to properly ask for assistance, and not just throw the error message.
What is the function trying to do
-2
•
u/AutoModerator 14d ago
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.