This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I've got java.lang.ArrayIndexOutOfBoundsException: null exception in my application two times, both of them happened at list.get() method, the JDK source like this
`private E get(Object[] paramArrayOfObject, int paramInt)
{
return paramArrayOfObject[paramInt];
}
`
What's the problem?
please give some simple example which throws java.lang.ArrayIndexOutOfBoundsException null.
Thanks!
Please check array length before calling paramArrayOfObject[paramInt];
if(paramArrayOfObject.length > paramInt)
return paramArrayOfObject[paramInt];
else
return null;
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm trying to filter from the list objects that field isActive have either set to N or null. Unfortunately, I get NullPointerException in method filter and I don't know what is wrong?
Code:
...
return dictionary.getAllPermissions().stream()
.filter(Objects::nonNull)
.filter(z->"N".equals(z.getIsActive().toString()) || z.getIsActive().equals(null)) //field isActive is Character
.collect(Collectors.toList());
You've got the ordering wrong, it should be:
.filter(z -> z.getIsActive() == null || "N".equals(z.getIsActive().toString()))
You should invert the checks here:
.filter(z->"N".equals(z.getIsActive().toString()) || z.getIsActive().equals(null))
to
.filter(z-> z.getIsActive() == null || "N".equals(z.getIsActive().toString()))
The idea is to first make sure that the value z.getIsActive() is not null before you can actually invoke the tostring() method to it.
z.getIsActive().toString() throws a NullPointExeption when isActive is null.
You said "field 'isActive' have either set to N or null", so if it's null all your z.getIsActive are null, so you need to check z.getIsActive==null
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I tried making a ticket using:
Ticket ticket = ForgeChunkManager.requestTicket(this, this.minecraftServer.entityWorld, ForgeChunkManager.Type.NORMAL);
The above code is in my main mod class. I get a NullPointerException when I try to run my mod.
in this case, either this.minecraftServer or this.minecraftServer.entityWorldis null.
try surrounding it with a if statement.
if (this.minecraftServer != null && this.minecraftServer.entityWorld != null){
Ticket ticket = ForgeChunkManager.requestTicket(this,
this.minecraftServer.entityWorld,
ForgeChunkManager.Type.NORMAL);
}
and for the purpose of debugging, I would suggest you separate it in two conditions:
if (this.minecraftServer == null) {
System.out.println("minecraftServer is null");
//or do anything can to warn you
} else if(this.minecraftServer.entityWorld == null) {
System.out.println("entityWorld is null");
//or do anything can to warn you
} else {
Ticket ticket = ForgeChunkManager.requestTicket(this,
this.minecraftServer.entityWorld,
ForgeChunkManager.Type.NORMAL);
}
Unless you can use a debugger to check the values.
But without the full code, it's impossible to know if there's any other error.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have two cursors which are used to display details in UI.
headerCur = (ResultSet) cstmt.getObject(4);
serialCur = (ResultSet) cstmt.getObject(5);
When the user enters a wrong value, it goes to serialCur.next().
while (serialCur.next()) {
// ...
}
However, it throws a NullPointerException.
I have logged and found that the serialCur is not null, but the next() call throws null pointer exception anyway.
How is this caused and how can I solve it?
Use
while (serialCur.isBeforeFirst() ) {
// ...
}
OR
if(serialCur.isBeforeFirst()) {
while (serialCur.next() ) {
// ...
}
}
to check if result set has a value
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a method that return an array of files in a given directory that is giving me a null pointer exception when executed. I can't figure out why.
private ArrayList<File> getFiles(String path) {
File f = new File(path);
ArrayList<File> files = new ArrayList<>(Arrays.asList(f.listFiles()));
return files;
}
thanks for your help
This NullPointerException is thrown when the path specified while initializing the file is incorrect (doesn't exist).
In such cases it is always advisable to add some null checks(protective code) in your method.
eg:
if( f != null) { //get the list of files }
may be casue by f.listFiles() return one null array.you can watch the variables in debug model
This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Closed 7 years ago.
my Java (libgdx) application/game throws me java.util.ConcurrentModificationException.
My code:
for (Lod lod : lode) {
if (!pause){
lod.move();
}
if (lod.isDestroyed()){
lode.remove(lod);
} else {
lod.draw(game.batch);
}
}
You try to remove an element while you are iterating over the collection. This is simply not possible by the iterator.