2

I'm trying to do a simple software that will draws a rectangle and some rows but i have a unexpected java.lang.NullPointerException when i try to add my panel (extended to JPanel) at my JFrame. The code is:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.Entity.robot.Map;




import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;



public class MainWindow {
private JFrame frame;
private JFileChooser fileChooser;
private MapGUI panel;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    panel= new MapGUI();
    panel.setBounds(200, 100, 500, 250);
    frame.getContentPane().add(panel); <---- THE EXCEPTION IS HERE**
    frame = new JFrame();
    frame.getContentPane().setLayout(null);
    frame.setResizable(false);
    frame.setBounds(100, 100, 1500, 900);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

         JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmOpenMapFrom = new JMenuItem("Open map from...");
        mnFile.add(mntmOpenMapFrom);
    }

}



class MapGUI extends JPanel {

    public MapGUI(){
        setPreferredSize(new Dimension(300, 300));
    }

    public void paint (Graphics g){
        g.setColor(Color.white);
        g.drawRect(1, 1, 500, 250);


    }

}

How can i fix it?

2 Answers 2

2
frame.getContentPane().add(panel); <---- THE EXCEPTION IS HERE**
frame = new JFrame();

In the above code, you haven't defined what frame is until the line after the exception. Switch their order:

frame = new JFrame();
frame.getContentPane().add(panel);
Sign up to request clarification or add additional context in comments.

Comments

2

The reason of NullPointerException is that you are trying to call a method on uninitialized/null frame object. In your code you need to initalize your frame object prior using it.

Just reverse these statements:

  frame.getContentPane().add(panel); <---- THE EXCEPTION IS HERE**
  frame = new JFrame();

to

   frame = new JFrame();
   frame.getContentPane().add(panel); <---- THE EXCEPTION IS HERE**

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.