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
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