Hej alle! Har et problem med min kode, får NullPointerException..
Får min fejl i den her linje:
- public void paintBG(Graphics g)
- {
- g.drawImage(window.bg, 1, 1, null); // BACKGROUND DET ER HER JEG FÅR FEJL!
- g.drawImage(window.charles, window.charlesx, window.charlesy, null); // CHARACTER CHARLES!!
- }
Får dette output:
Hello Woindow
Exception in thread "main" java.lang.NullPointerException
at Panel.paintBG(Panel.java:24)
at Window.<init>(Window.java:66)
at Window.main(Window.java:102)
Her er al min kode:
Window.java
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.File;
- import java.io.IOException;
-
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Window extends JFrame
- {
- public static Window window;
- Panel panel;
- Keylistener keylistener;
- Graphics g;
-
- int windowx, windowy; // Window size
- int charlesx, charlesy; // Where charles is positioned
- // For the prompt
- String command = "";
- String outputString = "";
- JTextArea output; // Output, where the response will get shown i:
- JLabel input; // For the commands
-
-
- public Image bg, charles, pants, shirt, socks, tanktop, underpants, wig;
-
-
- public Window() // ONLY MAKE ONE OBJECT OF EACH CLASS!!!!
- {
-
- keylistener = new Keylistener(window, panel);
- //g = panel.getGraphics();
- panel = new Panel(window);
-
- // For the output stupid
- output = new JTextArea();
- output.setFocusable(false);
- output.setForeground(Color.WHITE);
- output.setBackground(Color.BLACK);
- output.setEditable(false);
- output.setText(outputString);
- output.setLineWrap(true);
-
- System.out.println("Hello Woindow");
- windowx = 1000;
- windowy = 700;
-
- charlesx = 50;
- charlesy = 60;
-
- init();
-
- this.setVisible(true);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setSize(windowx, windowy);
- this.add(panel);
-
- panel.paintBG(g); // Putting the background to the screen
- }
-
- // Init pics
- public void init()
- {
- try
- {
- bg = ImageIO.read(new File("res/Background.png"));
- charles = ImageIO.read(new File("res/Charles.png"));
- pants = ImageIO.read(new File("res/Pants.png"));
- shirt = ImageIO.read(new File("res/Shirt.png"));
- socks = ImageIO.read(new File("res/Socks.png"));
- tanktop = ImageIO.read(new File("res/Tanktop.png"));
- underpants = ImageIO.read(new File("res/Underpants.png"));
- wig = ImageIO.read(new File("res/Wig.png"));
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
-
- // Refreshing the output for the prompt
- public void refreshOutput()
- {
- output.setText(outputString.toUpperCase());
- }
- // Refreshing the input for the prompt
- public void refreshInput()
- {
- input.setText("\n> " + command);
- }
-
- public static void main(String[] args)
- {
- window = new Window();
- }
- }
Panel.java
- import java.awt.Graphics;
- import java.awt.Image;
- import javax.swing.*;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Panel extends JPanel
- {
- Window window;
- public Panel(Window w)
- {
- window = w;
- }
-
- public void addClothes(Graphics g, Image img)
- {
- g.drawImage(img, window.charlesx, window.charlesy, null);
- }
-
- public void paintBG(Graphics g)
- {
- g.drawImage(window.bg, 1, 1, null); // BACKGROUND
- g.drawImage(window.charles, window.charlesx, window.charlesy, null); // CHARACTER CHARLES!!
- }
- }
Keylistener.java
- import java.awt.event.KeyListener;
- import java.awt.event.KeyEvent;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Keylistener implements KeyListener
- {
- boolean bpants, bshirt, bsocks, btanktop, bunderpants, bwig;
-
- Window window;
- Panel panel;
-
- public Keylistener(Window w, Panel p)
- {
- window = w;
- panel = p;
- }
-
- public void keyPressed(KeyEvent e)
- {
- int keyTyped = e.getKeyCode();
-
- if(keyTyped == KeyEvent.VK_1)
- {
- window.command += "1";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_2)
- {
- window.command += "2";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_3)
- {
- window.command += "3";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_4)
- {
- window.command += "4";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_5)
- {
- window.command += "5";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_6)
- {
- window.command += "6";
- window.refreshInput();
- }
- else if(keyTyped == KeyEvent.VK_7)
- {
- window.command += "7";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_8)
- {
- window.command += "8";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_9)
- {
- window.command += "9";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_0)
- {
- window.command += "0";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_COMMA)
- {
- window.command += ",";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_PERIOD)
- {
- window.command += ".";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_A)
- {
- window.command += "A";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_B)
- {
- window.command += "B";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_C)
- {
- window.command += "C";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_D)
- {
- window.command += "D";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_E)
- {
- window.command += "E";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_F)
- {
- window.command += "F";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_G)
- {
- window.command += "G";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_H)
- {
- window.command += "H";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_I)
- {
- window.command += "I";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_J)
- {
- window.command += "J";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_K)
- {
- window.command += "K";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_L)
- {
- window.command += "L";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_M)
- {
- window.command += "M";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_N)
- {
- window.command += "N";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_O)
- {
- window.command += "O";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_P)
- {
- window.command += "P";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_Q)
- {
- window.command += "Q";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_R)
- {
- window.command += "R";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_S)
- {
- window.command += "S";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_T)
- {
- window.command += "T";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_U)
- {
- window.command += "U";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_V)
- {
- window.command += "V";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_W)
- {
- window.command += "W";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_X)
- {
- window.command += "X";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_Y)
- {
- window.command += "Y";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_Z)
- {
- window.command += "Z";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_SPACE)
- {
- window.command += " ";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_BACK_SPACE)
- {
- int delete = window.command.length() - 1;
- window.command = window.command.substring(0, delete);
-
- window.refreshInput();
- }
-
- /* if(keyTyped == KeyEvent.VK_ENTER)
- {
-
- String addOutput
-
- window.outputString += "\n>>> ";
- window.outputString += addOutput;
- window.refreshOutput();
- window.command = "";
- window.refreshInput();
- } */
-
- }
-
- public void keyReleased(KeyEvent e)
- {
-
- }
-
- public void keyTyped(KeyEvent e)
- {
-
- }
-
- }
Indlæg senest redigeret d. 19.01.2013 12:28 af Bruger #16945