简体   繁体   中英

“Component cannot be instantiated” in Netbeans 7.0.1

EDIT: Sorry, I just started programming in Java. 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...

(using Netbeans 7.0.1)

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 this 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 and I can't solve it. Can anyone help me please?

Thanks a lot for this answer but, if you want to know the reason here you are.

Typically this appears on two position:

  1. an overridden method on your component.
  2. a normal method on your component.

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 supper class constructor then it the overridden new method would be called in the case of the (label) variable which is used on this method still no being initialized so a java.lang.NullPointerException will be thowed.

solution:

1) try ... catch :

@Override
public void setText(String text) {
    try {
        label.setText(text);
    } catch (Exception e) {
    }
}

2) 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);
}

3)use initialization on declaration:

private javax.swing.JLabel label = label = new javax.swing.JLabel();

and then use 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 initialize the variable or assign null to the variable before calling the method and so on and it can being solved by the same ways.

I have encountered the similar problem, however in different context.

I have two separate projects, a swing built user interface, and another one that poses as 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 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.

I too faced the same problem, 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 error msg, goto 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 msg 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.

Hope this helps..

Goodluck

reachSDK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM