user2158386
user2158386

Reputation: 25

i cant able to modify the value in label while using net beans

i'm new to Net Beans. When I try to run this I get an error like non static variable cannot be referenced from static context. Please help me to solve. but when i declare the Label inside the main, i can able to get output (in eclipse). net beans didn't allow to change variable declaration.

import java.net.InetAddress;

public class Test extends javax.swing.JFrame {

    public Test() {
        initComponents();
    } // 

    private void initComponents() {
        l1 = new javax.swing.JLabel();
        name = new javax.swing.JLabel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        l1.setText("System Name:");
        name.setText("jLabel2");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 21, Short.MAX_VALUE))
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(109, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(102, 102, 102))
                );
        pack();
    }// 

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
// 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
        try {
            ip = InetAddress.getLocalHost();
            name.setText(ip.getHostName());
        } catch (Exception e) {
        }
    }
    InetAddress ip;
// Variables declaration - do not modify 
    private javax.swing.JLabel l1;
    private javax.swing.JLabel name;
// End of variables declaration 
}

Upvotes: 0

Views: 106

Answers (2)

pundit
pundit

Reputation: 312

This error is not related to netbeans. Rather you are getting this as your are referencing the non static variables from static method.

So for short fix you can make your variables static. For long fix do not refer them from main method rather create separate method with the code that you have in main and call that method from main like new ClassName().methodName(); in this case there will be no need to declare variables static.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

This is basically telling that you are trying to reference some non-static variable from within a `static context.

Look at you main method. It's decalared as static, it does not depend on a given instance of a class to be called.

public static void main(String args[]) {
    /*...*/
    try {
        ip = InetAddress.getLocalHost();
        name.setText(ip.getHostName());
    } catch (Exception e) {
    }
}

InetAddress ip;
// Variables declaration - do not modify 
private javax.swing.JLabel l1;
private javax.swing.JLabel name;

But the variables you are trying to access are not (static), they relay on a particular instance of the class that they are declared in (AKA instance variables).

As it stands, the only choice you have is to move the offending piece of code into the constructor of the class or some other non-static method that you can call...

public Test() {
    initComponents();
    try {
        ip = InetAddress.getLocalHost();
        name.setText(ip.getHostName());
    } catch (Exception e) {
    }
} // 

Take the time to have a read through Understanding Instance and Class Members

You may also want to have a read through Code Conventions for the Java Programming Language

Upvotes: 1

Related Questions