ResultSet instance is not null, but next() call throws NullPointerException [duplicate] - java

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

Related

java.lang.ArrayIndexOutOfBoundsException: null [duplicate]

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;

Java Stream - NullPointExeption when filter list [duplicate]

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

Adding elements to an empty ArrayList<T> [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am adding objects to an ArrayList that is initialized as null inside a for each loop, but SonarLint is giving me a "NullPointerException" could be thrown when I try to add each object. Why is this error being pointed out?
public List<SatelliteData> getData()
{
SatelliteData satellite;
ArrayList<SatelliteData> satelliteList = null;
try(FileInputStream file = new FileInputStream(satelliteOutputFile))
{
TLEParser parsedFile = new TLEParser();
List<TLE> tleList = parsedFile.readFile(file);
for (TLE tle : tleList)
{
satellite = new SatelliteData(tle);
satellite.collectSatelliteData();
satelliteList.add(satellite); //A "NullPointerException" could be thrown; "satelliteList" is nullable here
}
}
catch (IOException ex)
{
LOGGER.error("IO Exception: " + ex);
notifyTheObservers("IO Exception: " + ex);
}
return satelliteList;
}
You shouldn't call member methods on variables that have been initialized to nothing/null.
In this case you have
ArrayList<SatelliteData> satelliteList = null;
This variable does not have any memory allocated to it.
At this point your computer knows : Okay there exists something
called satelliteData but it actually doesn't know where it is?
Calling add() method on it, is likely to throw a NullPointerException because this variable is a reference that is pointing to null/nothing.
To overcome this, You need to initialize the variable like this :
ArrayList<SatelliteData> satelliteList = new ArrayList<SatelliteData>();
At this point your computer created that satteliteData and knows
exactly where it exists hence it can happily operate upon it.
This will allocate some memory to this variable and now it has its own methods you can call.

Can't create a chunk loading ticket in Forge [duplicate]

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.

Not sure why this Java Null Pointer Exception is happening [duplicate]

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

Categories