Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Java GUI Error


Gipsy.D
HOME ยป Java

I am trying to design a calculator in java. My code isn't finished but I have designed a GUI, which works except for when i try to actually calculate the result. When i press "=" on my calculator i receive the above error. Here is my code:GUI:import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.*;import java.io.IOException;public class CalcGUI extends JFrame implements ActionListener { /* * GUI for calculator */// Create varia

Related to : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Java GUI Error
Java - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException error
5/12/2014 7:00 pm Java

I am creating simple game in java. Here is my code. This is my GUI class:

public class GUI extends JFrame implements ActionListener{

JButton upbutton, downbutton, leftbutton, rightbutton;

Game game;
Player player;
Board b;
Maze maze;
Player pl;

public GUI () {
    b= new Board();
    Game game = new Game(b);


    setSize(800,600);
    Container cp = getContentPane();

    upbutton = new JButton("UP");
    downbutton = new JButton("DOWN");   
    leftbutton = new JButton("LEFT");
    rightbutton= new JButton("RIGHT");
    upbutton.addActionListener(this);
    downbutton.addActionListener(this);
    leftbutton.addActionListener(this);
    rightbutton.addActionListener(this);

    JPanel p = new JPanel();
    BoxLayout boxLayout1 = new BoxLayout(p, BoxLayout.Y_AXIS);
    p.setBorder(BorderFactory.createEmptyBorder(250, 0, 250, 0));
    p.setLayout(boxLayout1);

    JPanel panel_1 = new JPanel();
    p.add(panel_1, BorderLayout.NORTH);

    JPanel panel_2 = new JPanel();
    p.add(panel_2);

    JPanel panel_3 = new JPanel();
    p.add(panel_3, BorderLayout.AFTER_LAST_LINE);

    panel_1.add(upbutton);
    panel_2.add(leftbutton);
    panel_2.add(rightbutton);
    panel_3.add(downbutton);

    cp.setLayout(new BorderLayout());
    cp.add(p, BorderLayout.EAST);

    game= new Game(b);
    cp.add(game, BorderLayout.CENTER);
}


public void actionPerformed(ActionEvent e) {
    if(upbutton == e.getSource()){
        game.movePlayerUp();
        System.out.println("up");
        }
    if(downbutton == e.getSource()){
        game.movePlayerDown();
        System.out.println("down");
        repaint();
        }
    if(leftbutton == e.getSource()){
        game.movePlayerLeft();
        repaint();
        System.out.println("left");
        }
    if(rightbutton == e.getSource()){
        game.movePlayerRight();
        repaint();
        System.out.println("right");
        }
    game.repaint();
    }
public static void main(String[] args) {
    GUI frame = new GUI();
    frame.setVisible(true);
}

}

This is my Game class:

public class Game extends Canvas {

int m;
int n;  // the margin
Board b;
double tokenSize, markingSize;
int[][] marking;
int pickupType;
int pickupX, pickupY;
Dimension lastSize;
Image picture;
Maze maze;
Player player;

boolean ingame = false; 
boolean dying = false; 
boolean finished = false;
Thread gamethread = null;

int pposition [][];
public int positiona;
public int positionb;

public final Font smallfont = new Font("Helvetica", Font.BOLD, 14);

public Game (Board db) {
    pposition = new int[4][4];
    maze = new Maze();
    b=db;
    m=20;
    setBackground(Color.lightGray);
    lastSize=getSize();
    picture=createImage(lastSize.width, lastSize.height);
    createPlayerBoard();
    setInitialPlayerPosition();
}

public void paint (Graphics g){
    update(g);
}

public synchronized void update(Graphics g1) {

    Dimension d=getSize();
    if (!d.equals(lastSize)) {
        picture=createImage(d.width, d.height);
    } 

    Graphics g=picture.getGraphics();

    g.setColor(getBackground());
    g.fillRect(0, 0, d.width, d.height);
    g.setColor(getForeground());
    g.drawRect(0, 0, d.width-1, d.height-1);
    int h=getHeight();
    int w=getWidth();

    int squareSize = Math.max(0,(Math.min(h,w)-2*m))/8;
    squareSize = squareSize+40;

    Graphics2D g2 = (Graphics2D) g;
    Image img1 = Toolkit.getDefaultToolkit().getImage("3down.jpg");
    Image img2 = Toolkit.getDefaultToolkit().getImage("3left.jpg");
    Image img3 = Toolkit.getDefaultToolkit().getImage("3right.jpg");
    Image img4 = Toolkit.getDefaultToolkit().getImage("3up.jpg");
    Image img5 = Toolkit.getDefaultToolkit().getImage("all4.jpg");
    Image img6 =
Toolkit.getDefaultToolkit().getImage("down_left.jpg");
    Image img7 =
Toolkit.getDefaultToolkit().getImage("down_right.jpg");
    Image img8 = Toolkit.getDefaultToolkit().getImage("up_left.jpg");
    Image img9 = Toolkit.getDefaultToolkit().getImage("up_right.jpg");
    Image img10 = Toolkit.getDefaultToolkit().getImage("cyclops.gif");

    //this code displays board

    for (int i=0; i<5; i++) {
        for (int j=0; j<5; j++) {
            if(i==0&&j==0){
            g2.drawImage(img7,m + i * squareSize, m + j * squareSize,
squareSize, squareSize, this);
            g2.finalize();}
            else if(i==1&&j==0){
            g2.drawImage(img1,m + i * squareSize, m + j * squareSize,
squareSize, squareSize, this);
            g2.finalize();}
            else if (i==2&&j==0){
            g2.drawImage(img1,m + i * squareSize, m + j * squareSize,
squareSize, squareSize, this);
            g2.finalize();}
            else if (i==3&&j==0){
            g2.drawImage(img1,m + i * squareSize, m + j * squareSize,
squareSize, squareSize, this);
            g2.finalize();}
            else if (i==4 &&j==0){
            g2.drawImage(img6,m + i * squareSize, m + j * squareSize,
squareSize, squareSize, this);
            g2.finalize();}
    }

    //this code will display the cyclop

    loadPlayerPosition();
    int x=positiona;
    int y=positionb;

    g2.drawImage(img10,m + x * squareSize, m + y * squareSize,
squareSize, squareSize, this);
    g2.finalize();

g1.drawImage(picture,0,0,null);
}
    //this method creates player board and fills all places in array
with 0
public void createPlayerBoard(){
    for (int i = 0; i < pposition.length; i++){
        for (int j = 0; j < pposition.length; j++){
                pposition[i][j] = 0;
        }
    }
}

//this method sets the initial player position in array
public void setInitialPlayerPosition(){
    pposition[0][0] = 1;

}

//this method changes the interger positiona and positib be, so
integers represent the current player position
public void loadPlayerPosition(){
    for (int i = 0; i < pposition.length; i++){
        for (int j = 0; j < pposition.length; j++){
            if(pposition [i][j] == 1){
                positiona=i;
                positionb=j;
                    }
        }
    }
}

//these methods change the number in player array from 0 to 1,
deppending on which button player clicked

public void movePlayerUp(){
    for (int i = 0; i < pposition.length; i++){
        for (int j = 0; j < pposition.length; j++){
            if(pposition [i][j] == 1){
                pposition [i][j] = 0;
                pposition [i][j+1] = 1;
                    }
        }
    }
}

public void movePlayerDown(){
    for (int i = 0; i < pposition.length; i++){
        for (int j = 0; j < pposition.length; j++){
            if(pposition [i][j] == 1){
                pposition [i][j] = 0;
                pposition [i][j-1] = 1;
                    }
        }
    }
}

public void movePlayerLeft(){
    for (int i = 0; i < pposition.length; i++){
        for (int j = 0; j < pposition.length; j++){
            if(pposition [i][j] == 1){
                pposition [i][j] = 0;
                pposition [i-1][j] = 1;
                    }
        }
    }
}

public void movePlayerRight(){
    for (int i = 0; i < pposition.length; i++){
        for (int j = 0; j < pposition.length; j++){
            if(pposition [i][j] == 1){
                pposition [i][j] = 0;
                pposition [i+1][j] = 1;
                    }
        }
    }
}

}
This is my board class:

public class Board {

int[][] b;
Maze maze;
boolean alive = true;
public int playerscore = 0;
Maze m;



public Board() {

    }

}

The program normally creates the windows with buttons, displays the board and displays the player icon. What I want to do is: when player clicks on right button, the icon moves right on the array. When I click on the button, I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

I try to search the error and it says this happens when the program points to object which is null.However, I think my object is not null. Can somebody give me advice please? Thank you very much


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Java GUI Error
Java

I am trying to design a calculator in java. My code isn't finished but I have designed a GUI, which works except for when i try to actually calculate the result. When i press "=" on my calculator i receive the above error. Here is my code:GUI:import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.*;import java.io.IOException;public class CalcGUI extends JFrame implements ActionListener { /* * GUI for calculator */// Create varia
Java Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException error
Java

I have tried to go over this my self but i do not know how to resolve this.


I am getting this error when i try and run my addressBookUi class.


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at addressBookUI.startComponents(addressBookUI.java:43)
at addressBookUI.<init>(addressBookUI.java:6)
at addressBookUI$2.run(addressBookUI.java:143)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.ru
Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
Java

public class MainFrame extends JFrame {
public DefaultTableModel historyModel;
public DefaultTableModel dataModel;
public JTable historyTable;
public JTable dataTable;
public JTableHeader header;
private void createUI() {
browsersTabbedPane = new BrowsersTabbedPane();
add(browsersTabbedPane.getTabPane(), CENTER);
addDefaultBrowserComponentForTabbedPane();
createActions();
add(createToolBar(), BorderLayout.NORTH);
add(createStatusBar(), BorderLayout.SOUTH);
add(createDataView(dataModel,dataTable, header), BorderLayout.PAGE_END);


HD Wallpapers
3D
3D Abstract
City
Celebrities
Indian Celebrities
Cars
Travel
Girls
Animals Birds
Movies
Sports
Black White
Nature
Planes
Anime
Food Drink
Dreamy Fantasy
Architecture
Games
Space
Holidays
Flowers
Love
Artistic
Baby
Beach
Bikes Motorcycles
Macro
Computers
Vector
Funny
Army
Textures
Brands
Misc
Music
Other
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .