Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intelliji Hallo World sample program error

Status
Not open for further replies.

fawaz

Programmer
Mar 3, 2001
16
IN
I am new to Java and I am writing a simple prg to display msg in intelliji; The code and error is as below. Please advise. thanks.

main.java
--------------
public class Main {


public static void main(String[] args) {

System.out.println("bismillah");

JFrame frame = new JFrame("App");
frame.setContentPane(new App().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();
frame.setVisible(true);

}



App.java
-----------

package sample;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class App {
public JButton buttonMsg;
public JPanel panelMain;

public App() {
buttonMsg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"HALLO WORLD");
}
});
}

private void createUIComponents() {
// TODO: place custom component creation code here
}


}


The Error msg is:

Hallo World
Exception in thread "main" java.lang.NullPointerException
at sample.App.$$$setupUI$$$(App.java)
at sample.App.<init>(App.java:12)------ buttonMsg.addActionListener(new ActionListener() {
at sample.Main.main(Main.java:53)----------frame.setContentPane(new App().panelMain);

Process finished with exit code 1
 
In Java, objects need to be created, primitive types don't. For instance
Code:
// This will work
int x;
x = 20;

// This won't work
JButton noworkies;
noworkies.addActionListener(...);

// It needs to be created first
JButton workies = new JButton(...);  // No idea what the creation params are
workies.addActionListener(...);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top