Reputation: 923
I have been customizing JTextArea and JTable. I do so by adding a new Java class to my project and then declaring it extends the particular class I want (in my case, either JTextArea or JTable).
I had been using it normally, adding these new classes to JDialogs and JInternalFrames without any problem. I do so by just dragging it to my JDialog or JInternalFrame...
But recently, for some reason, I started getting these error messages:
Component cannot be instantiated. Please make sure it is a JavaBeans component.
The JInternalFrames that were accepting the old customized classes still accepts them. But if I try to add the new customized class, it gives me that error message and, afterwards, it starts showing the same message to the old customized classes too...
Something really weird is going on. I copied the same code of a (previously) customized class to a new class (changing the name of the class, of course). Then I try to add this to my JInternalFrame. It gives me the error message! If, before this, I try to add the same customized class (with the original name), it adds the class normally...
This is annoying. How can I solve?
(I am using NetBeans 7.0.1)
It turned out to be a problem with an out-of-range array access... I am used to error messages about this kind of thing being automatic... Sorry, I just started programming in Java.
Upvotes: 2
Views: 6759
Reputation: 31
Thanks a lot for that answer, but if you want to know the reason, here you are.
Typically this appears on two positions:
For example:
package UI.Components;
public class LabelComponent extends javax.swing.JLabel {
private javax.swing.JLabel label;
public TextFieldComponent() {
label = new javax.swing.JLabel(_label);
add(label);
}
@Override
public void setText(String text) {
label.setText(text);
}
}
The method setText(String text)
is called say in the super class constructor, and then it is the overridden new method that would be called in the case of the (label) variable which is used on this method still not being initialized, so a java.lang.NullPointerException
will be thrown.
Solution:
try
... catch
:
@Override
public void setText(String text) {
try {
label.setText(text);
} catch (Exception e) {
}
}
Check:
Use null initialization on declaration
private javax.swing.JLabel label = null;
Then check on the method
@Override
public void setText(String text) {
if(label != null)
label.setText(text);
}
Use initialization on declaration:
private javax.swing.JLabel label = label = new javax.swing.JLabel();
and then use the setText method in your constructor
label.setText(_label);
Note:
In the case of reason (2), a normal method on your component, it is the same as (1), but you may call the method before initializing the variable or assign null to the variable before calling the method and so on and it can be solved by the same ways.
Upvotes: 2
Reputation: 91
I too faced the same problem, and after some search in the web I found the solution for this problem. I don't have a deep understanding of why and how this problem occurs, but I can share with you the solution I found.
When you get such an error message, go to the menu View → IDE Log or you can open the log
from windows_user_Home\\.netbeans\7.0\var\log
.
In that log, you have to locate the error message you got, for example,
INFO [org.netbeans.modules.form.BeanSupport]: Cannot create default instance of: test.Application1
java.lang.NullPointerException
at test.Application1.initLabel(Application1.java:906)
So the problem is in line 906 of your .java file. Open that file and comment those lines and then you will able to overcome the problem.
You can add the component to the Form or jInternalFrame or...
After adding the component, you can again uncomment those lines. Just Clean and Build your project.
Upvotes: 0
Reputation: 1
I have encountered the similar problem, however in a different context.
I have two separate projects, a Swing built user interface, and another one that poses as a class library.
I added a class to the class library, headed over to the user interface, and implemented this newly added class from the library into the Swing interface project in the shape of an existing custom JFrame. So what happened to me now that the class loader of course could not find the class because the library project required compiling. The issue was fixed by compiling it.
Upvotes: 0