how to check jTable nullpointer in row [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
how to check my jtable if null pointer?
my code is always null pointer and then can't export to xls.
this my code
if (tbGudangSales.getValueAt(i, 5).toString().trim().length() == 0) {
status = "tidak ada penjualan";
} else{
status = tbGudangSales.getValueAt(i, 5).toString();
}
label = new Label(4, baris, status, bordertabel);
excelSheet.addCell(label);
this my error
this my value table

All you need to do is to check if the value is null. For example:
Object value = tbGudangSales.getValueAt(i, 5);
if (value != null)
{
if(value.toString().trim().isEmpty()) // instead checking the length "manually", you can use the isEmpty() method
{
status = "tidak ada penjualan";
}
else
{
status = value.toString();
}
}

Related

JUnit java.lang.NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Im new to software testing, i keep getting java.lang.NullPointerException for some reason and i don't know what it is
This is what i am testing on
public int getNumberOfStudents()
{
int validStudents = 0;
int i = 0;
boolean done = false;
do {
if (students[i] != null && students[i].exists())
validStudents++;
else
done = true;
i++;
} while(i < students.length && !done);
return validStudents;
} // end of getNumberOfStudents
This is the test case
class Testing {
RegistrationSystem GNS = new RegistrationSystem();
#Test
//#SpiraTestCase(testCaseId=5487)
public void test() {
assertEquals("validStudents", GNS.getNumberOfStudents());
}
}
I guess at line if (students[i] != null && students[i].exists()) you have not initialized students while creating RegistrationSystem Object, create it and it will work also I can see a problem in assert that you are comparing string with int :)assertEquals("validStudents", GNS.getNumberOfStudents());
Where exactly are you getting error.
One problem with your test case is you are comparing string "validstudents" with integer returned from function GNS.getNumberOfStudents(). You need to set a number of students, then call getNumberOfStudents() assert it with the expected value.

Check Boolean if null [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
Boolean valid = null;
if (valid == null) {
log.info("******");
}
I have the expression above, I wanted to check if Boolean value is null and if it is then execute statement inside if clause, but what happened is that it throws me a NullPointerException. What should I do to have my if clause evaluated with that given condition valid == null.
Thanks in advance
For sure, your log object contains null reference. You can't invoke any method with null reference. Try this:
Boolean valid = null;
if(valid == null) {
System.out.println("Hello");
}

why it result in a NullPointerException when Object is null? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
when objec is null:
if(object != null && object.string.equals("")) {
System.out.println("no error");
}
it will result in NullPointerException, and why if it checks the first result is false, it still check the second instead of stop check and print "no error"?
sorry for my bad english -_-#
It's not object which is null but object.string.
Try: (object != null && "".equals(object.string))

NullPointerException despite specifying [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I want to code a personal modloader for minecraft (create files , download files, sort files, etc) and the first window which is created is a JOptionPane which asks for a Version (i do not code in java really long, i don´t use spinner, just a "msg dialog" asking for a version). The code is:
public JOptionPane version = new JOptionPane();
public String modversion;
public Version()
{
showVersion();
}
public static void main(String[] args)
{
}
public void showVersion()
{
//input = version
String vers = version.showInputDialog("Welche Version soll modifiziert werden?");
if (vers.equals(null)) {
return;
} else {
if(vers.equals("1.5.2") || vers.equals("1.6.2") || vers.equals("1.6.4") || vers.equals("1.7.2") || vers.equals("1.7.10") || vers.equals("1.8") || vers.equals("1.8.9") || vers.equals("1.9") || vers.equals("1.10.2") || vers.equals("1.11.2"))
{
//mod version is saved as String (title for the config list)
modversion = vers;
return;
} else {
// with incompatible input the method will be repeated
JOptionPane.showMessageDialog(null, "Diese Version wird leider nicht supportet");
showVersion();
}
}
}
If you just press "Ok", the input would equal "null":
if (vers.equals(null)) {
return;
}
but it don´t quit the method, it says NullPointerException. Why doesn´t it just quit the method?
It's correct that you get an exception here.
Here:
if (vers.equals(null)) {
You are trying to call a method on a variable that is null. As you said yourself, vers is null at this point.
To check for null you have to use the == operator.
if(vers == null) { return;}
You can't call .equals() on a null object.
To check if the vers variable is null, you should use vers == null

AVL TREE REBALNCE [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
in Line 7 i get nullpointerexception, how can i fix that ? any ideas?
private void updateHeights(BSTreeNode v) {
BSTreeNode u = v;
while (u != null) {
int bfc=updateNodeHeight(u);
u = u.parent;
if (bfc<=-2) {
if( getHeight(u.left.left) >= getHeight(u.left.right) ) { // Null Pointer Exception
u = rotateRight(u);
} else {
u=LR(u);
}
}
}
}
There are two possibel problems, and as many solutions. First is placing a null check in getHeight() which I assume has been done based on your programming style (the check in while conditional). That leaves a possible null pointer issue with u.left. As such:
Ensure u.left is not null, like this:
getHeight(u.left != null && ((u.left.left)>=getHeight(u.left.right))

Categories