Java null pointer exception, when using JButton [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
code 1:
String s = Character.toString(button[0]);
bt[0].setText(s);//bt[]: JButton array;, button[] = char array
code 2:
String s = Character.toString(button[0]);
bt[0] = new JButton(s);
Suppose, I already initialize bt's by new JButton[num];
code 1 leads to null pointer exception, but code 2 run well.
I think bt's already has a pointer value of null. then, i can modify them without restriction. but it isn't.
I think code 2 initialize 2 times. but, it hasn't to be.

The problem is you initialized bt with new JButton[num];. But its content is still null, so bt[0] is null... So you try to run the method setText on a null reference.

Related

How to avoid the Null Pointer Exception from crashing my app? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
ret_idx might be null for a new user since its entry in database is not made. So whenever the app detects ret_idx is null, it shows the java.lang.NullPointerException. How do I avoid this exception from crashing my app, considering ret_idx might go null in several cases.
String ret_idx=dataSnapshot.child("name").getValue().toString();
Check whether your input data is null or not and if not then assign it to ret_idx.
if(dataSnapshot.child("name").getValue() != null){
String ret_idx=dataSnapshot.child("name").getValue().toString();
}

Print when espression - check if is empty array [duplicate]

This question already has answers here:
how to check for an empty array java [duplicate]
(6 answers)
Closed 4 years ago.
In JasperReport I want to print a field when it does not contain empty array.
How can I check it?
In "Print when expression" it is possible to write:
$F{myField} != null
but as $F{myField} is an empty array (so it is not null) it does not work.
Is there any method to check it? It seems to me that there is no isEmpty() function for JasperReport.
Try write something like:
$F{myField}.length() == 0

what will happen if I change the value of int in Java [duplicate]

This question already has answers here:
When changing the value of a variable in C, is a new primitive created or is the current primitive mutated?
(2 answers)
Closed 4 years ago.
I have a very basic, silly question.
Let's say in a function foo(), I put:
int a = 3; // after this, I know it will allocate a space in stack and put 3 in it.
Then, I put a = 5; // Here is my question:
What happened after I put a = 5?
Will a new space is allocated and put 5 in it? Or it will find out the new space in stack and then put 5 in it?
No it will not create new space just change value but if you pass this variable to a function will create new space as argument

Simple check operation is not working in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality

Trying to write java code that allows me to check status of POBox and change name of box owner [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am not sure what the issue is when I enter a box for the first choice a it returns
Because you have not initialize box[0]. You only allocated the space of the array.
You should do something like:
box[0] = new PO();
Remember that 'new' the array doesn't mean you 'new' the object. The array you use is to store the references(pointers) of objects.
Also, to improve the evolvability, please use a dynamic array such as ArrayList. Since the size of array is fixed once you create it.
PO[] box = new PO[nBoxes];
This line here is creating an array of references of class-type PO.
You need to allocate memory or create an instance using new.
For each reference in the array you must do this else the references point to null.
for(int i=0;i < nBoxes;i++) {
box[i] = new PO(); }

Categories