Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:15:10+00:00 2026-05-26T16:15:10+00:00

I keep getting a NullPointerException when I call the method I created, draw(), in

  • 0

I keep getting a NullPointerException when I call the method I created, draw(), in the constructor. It’s especially frustrating because I found a way around it but it’s not what I want. This is the code that works.
public class TutorialGrid extends JFrame{

private JPanel contentPane;
private Graphics g;
private int currentlength;
private int currentwidth;
private Integer[][] maze = new Integer[20][30];
private static TutorialGrid frame;
public static JTable table;
private JTextField title;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new TutorialGrid();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public TutorialGrid(){
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(0, 0, 1366, 768);
    contentPane = new JPanel();
    contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    ImageIcon icon = new ImageIcon("C:\\Users\\Brendan\\Desktop\\GAME\\Images\\BG7.jpg");
    input();


    table = new JTable();
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            draw(maze);
        }
    });
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setAutoCreateRowSorter(true);
    table.setCellSelectionEnabled(true);
    table.setColumnSelectionAllowed(true);
    table.setDoubleBuffered(true);
    table.setDragEnabled(true);
    table.setFillsViewportHeight(true);
    table.setFocusCycleRoot(true);
    table.setFocusTraversalPolicyProvider(true);
    table.setIgnoreRepaint(true);
    table.setInheritsPopupMenu(true);
    table.setSurrendersFocusOnKeystroke(true);
    table.setBackground(new Color(0, 0, 0));
    table.setForeground(new Color(255, 255, 255));
    table.setBorder(new LineBorder(new Color(139, 0, 0)));
    table.setBounds(180, 40, 1000, 600);
    contentPane.add(table);

    title = new JTextField();
    title.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            draw(maze);
        }
    });
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setText("Click to Start");
    title.setBorder(new LineBorder(new Color(0, 128, 0)));
    title.setEditable(false);
    title.setForeground(Color.WHITE);
    title.setBackground(new Color(0, 0, 0));
    title.setBounds(606, 11, 151, 20);
    contentPane.add(title);
    title.setColumns(10);
    JLabel lblBgpanel = new JLabel("", icon,JLabel.CENTER);
    lblBgpanel.setBounds(0, 0, 1360, 740);
    contentPane.add(lblBgpanel);
}

As you can see I have mouseListeners on both the table and the title which call the draw method successfully no problems at all. It draws the grid I want on the table, however in order to draw it I have to click one of those containers. I want it to draw the grid when the JFrame is initialized. But if I simply put draw(maze); in the constructor it gives me a null pointer exception. This is the code for both the methods draw and input which are used to draw the grid.

        public void draw(Integer[][] maze){
    int x= 125;
    int y =50;
    int width1 =25;
    int length1 =25;
    g=table.getGraphics();
    for(int i=0; i<20; i++)
       {

         for(int j=0; j<30; j++)
         {

             if(maze[i][j] == maze[currentlength][currentwidth])
             {
                g.setColor(Color.YELLOW);
                g.fillRect(x,y,width1,length1);
                g.setColor(Color.RED);
                g.drawRect(x,y,width1,length1);
                x = x+25;
             }
             else if(maze[i][j] == 1)
             {
                    g.setColor(Color.BLACK);
                    g.fillRect(x,y,width1,length1);
                    g.setColor(Color.RED);
                    g.drawRect(x,y,width1,length1);
                    x = x+25;
             }

                else if(maze[i][j] == 0)
                {
                    g.setColor(Color.BLUE);
                    g.fillRect(x,y,width1,length1);
                    g.setColor(Color.RED);
                    g.drawRect(x,y,width1,length1);
                    x = x+25;
                }
                else if(maze[i][j] == -2)
                {
                    g.setColor(Color.GREEN);
                    g.fillRect(x,y,width1,length1);
                    g.setColor(Color.RED);
                    g.drawRect(x,y,width1,length1);
                    x = x+25;
                }
                    else if(maze[i][j] == -10)
                    {
                        g.setColor(Color.WHITE);
                        g.fillRect(x,y,width1,length1);
                        g.setColor(Color.RED);
                        g.drawRect(x,y,width1,length1);
                        x = x+25;
                    }
         }
         y=y+25;
         x=125;
       }
}

        public void input(){
    //Imports and reads grid file
       Scanner scan = null;

       try
       {

           FileReader grid = new FileReader("C:\\Users\\Brendan\\Desktop\\tutorialgrid.txt");
           scan = new Scanner(grid);


       }
       catch(FileNotFoundException e)
       {
           System.out.println(e.getMessage() + "Could not find that file");
           System.exit(0);
       }

       for(int i=0; i<20; i++)
       {
         for(int j=0; j<30; j++)
         {
            maze[i][j]=scan.nextInt();
            if(maze[i][j] == -1)
            {
                currentlength = i;
                currentwidth = j;
            }
            if(maze[i][j] == -10)
            {
            }
         }
       }
}

}

All this is inside the same class. This is what I’m trying to do but gives me an error. I add the draw(maze) at the bottom of the constructor and it blows up on me the moment I try to run it.

    public TutorialGrid(){
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(0, 0, 1366, 768);
    contentPane = new JPanel();
    contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    ImageIcon icon = new ImageIcon("C:\\Users\\Brendan\\Desktop\\GAME\\Images\\BG7.jpg");
    input();


    table = new JTable();
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            draw(maze);
        }
    });
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setAutoCreateRowSorter(true);
    table.setCellSelectionEnabled(true);
    table.setColumnSelectionAllowed(true);
    table.setDoubleBuffered(true);
    table.setDragEnabled(true);
    table.setFillsViewportHeight(true);
    table.setFocusCycleRoot(true);
    table.setFocusTraversalPolicyProvider(true);
    table.setIgnoreRepaint(true);
    table.setInheritsPopupMenu(true);
    table.setSurrendersFocusOnKeystroke(true);
    table.setBackground(new Color(0, 0, 0));
    table.setForeground(new Color(255, 255, 255));
    table.setBorder(new LineBorder(new Color(139, 0, 0)));
    table.setBounds(180, 40, 1000, 600);
    contentPane.add(table);

    title = new JTextField();
    title.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            draw(maze);
        }
    });
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setText("Click to Start");
    title.setBorder(new LineBorder(new Color(0, 128, 0)));
    title.setEditable(false);
    title.setForeground(Color.WHITE);
    title.setBackground(new Color(0, 0, 0));
    title.setBounds(606, 11, 151, 20);
    contentPane.add(title);
    title.setColumns(10);
    JLabel lblBgpanel = new JLabel("", icon,JLabel.CENTER);
    lblBgpanel.setBounds(0, 0, 1360, 740);
    contentPane.add(lblBgpanel);
    draw(maze);
}

Here’s the error.

    java.lang.NullPointerException
at game.TutorialGrid.draw(TutorialGrid.java:136)
at game.TutorialGrid.<init>(TutorialGrid.java:111)
at game.TutorialGrid$1.run(TutorialGrid.java:41)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The game.TutorialGrid.draw line 136 is the line where the draw method starts.
The game.TutorialGrid. line 111 is last line in the constructor where I put draw(maze).
The game.TutorialGrid$1.run line 41 is the line frame = new TutorialGrid();

Help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:15:11+00:00Added an answer on May 26, 2026 at 4:15 pm

    getGraphics() returns null until things are visible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write program and keep getting a nullPointerException when I call
I have a about screen that has some buttons, but I keep getting NullPointerException
Im trying to display this listview, but I keep getting a: 07-17 21:14:22.233: ERROR/AndroidRuntime(349):
I've got a hibernate query I'm trying to get working but keep getting an
Keep getting The constructor SimpleCursorAdapter(MyProgram, int, Cursor, int) is undefined and am not sure
I keep getting a 404 Page Not Found whenever I try to access CodeIgniter's
I keep getting NullPointerException trying to deflate or make invisible the ViewStub from my
I keep getting Activity Monitor Job. java.lang.NullPointerException while working with Eclipse 3.6 Has anybody
I keep getting this error 07-14 23:53:03.653: ERROR/AndroidRuntime(14995): java.lang.NullPointerException 07-14 23:53:03.653: ERROR/AndroidRuntime(14995): at com.fttech.organizeit.meeting_list$meetingHolder.populateFrom(meeting_list.java:110)
I keep getting comet when I search for this, but comet seems to complex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.