Results 1 to 10 of 10
  1. #1
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Help please? Eclipse

    There is a problem with my eclipse, i enter the code right and run it and it shows me this error:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at rtypeAnim.Craft.<init>(Craft.java:25)
    at rtypeAnim.Board.<init>(Board.java:28)
    at rtypeAnim.RType.<init>(RType.java:9)
    at rtypeAnim.RType.main(RType.java:20)


    those involve those problems:
    ImageIcon ii = new ImageIcon(this.getClass().getResource(craft)); //craft is a var containing the link
    craft = new Craft(); // Call Class
    add(new Board()); // Call Class
    new RType(); // Call Function

    but when i restart my PC they work fine like nothing was wrong... restarting Eclipse or re-logging does not fix this problem...
    I say that the problem is with the calling function in the builder, but i dont know how to fix it, so please help
    Thank you

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help please? Eclipse

    What is 'craft'?
    What does it reference?
    Does whatever it references move, change, whatever?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Help please? Eclipse

    Posting the whole code here will make things hard to figure out... but i'll do it anyway


    Main:
    Java Code:
    package rtypeAnim;
    
    import javax.swing.JFrame;
    
    public class RType extends JFrame {
    
        public RType() {
    
            add(new Board());
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            setTitle("R - Type");
            setResizable(false);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new RType();
        }
    }

    Craft:

    Java Code:
    package rtypeAnim;
    
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    
    public class Craft {
    
        private String craft = "craft.png";
    
        private int dx;
        private int dy;
        private int x;
        private int y;
        private Image image;
    
        private ArrayList missiles;
    
        private final int CRAFT_SIZE = 76;
    
        public Craft() {
            ImageIcon ii = new ImageIcon(this.getClass().getResource("craft.png"));
            image = ii.getImage();
            missiles = new ArrayList();
            x = 40;
            y = 60;
        }
    
    
        public void move() {
            x += dx;
            y += dy;
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    
        public Image getImage() {
            return image;
        }
    
        public ArrayList getMissiles() {
            return missiles;
        }
    
        public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_SPACE) {
                fire();
            }
    
            if (key == KeyEvent.VK_LEFT) {
                dx = -1;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                dx = 1;
            }
    
            if (key == KeyEvent.VK_UP) {
                dy = -1;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                dy = 1;
            }
        }
    
        public void fire() {
            missiles.add(new Missile(x + CRAFT_SIZE, y + 69/2));
        }
    
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_LEFT) {
                dx = 0;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                dx = 0;
            }
    
            if (key == KeyEvent.VK_UP) {
                dy = 0;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                dy = 0;
            }
        }
    }
    Board:
    Java Code:
    package rtypeAnim;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    public class Board extends JPanel implements ActionListener {
    
        private Timer timer;
        private Craft craft;
    
        public Board() {
    
            addKeyListener(new TAdapter());
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
    
            craft = new Craft();
    
            timer = new Timer(5, this);
            timer.start();
        }
    
    
        public void paint(Graphics g) {
            super.paint(g);
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
    
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
    
        public void actionPerformed(ActionEvent e) {
            craft.move();
            repaint();  
        }
    
    
        private class TAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
                craft.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                craft.keyPressed(e);
            }
        }
    
    }
    Missile:
    Java Code:
    package rtypeAnim;
    
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    
    public class Missile {
    
        private int x, y;
        private Image image;
        boolean visible;
    
        private final int BOARD_WIDTH = 390;
        private final int MISSILE_SPEED = 2;
    
        public Missile(int x, int y) {
    
            ImageIcon ii =
                new ImageIcon(this.getClass().getResource("missile.png"));
            image = ii.getImage();
            visible = true;
            this.x = x;
            this.y = y;
        }
    
    
        public Image getImage() {
            return image;
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    
        public boolean isVisible() {
            return visible;
        }
    
        public void move() {
            x += MISSILE_SPEED;
            if (x > BOARD_WIDTH)
                visible = false;
        }
    }
    Error:
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at rtypeAnim.Craft.<init>(Craft.java:25)
    at rtypeAnim.Board.<init>(Board.java:28)
    at rtypeAnim.RType.<init>(RType.java:9)
    at rtypeAnim.RType.main(RType.java:20)

    at Missile line 19 there is nothing wrong... however in Craft line 25 there is...
    both are the same and both pics are at the same directory, couldn't figure it out?
    i had the same problem earlier, and restarting my PC magicly solved it... that's why i thought there was a problem with the Compiler... but i guess there isn't and it's code related?
    Last edited by bakriawad; 04-16-2013 at 05:12 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help please? Eclipse

    So 'craft.png' is not in the 'rtypeAnim' package, if I've read that correctly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Help please? Eclipse

    yh.. i guess you was right! i don't know how i missed that even though i rechecked it many times... i remember saving it as .png but it was saved as .jpg
    sorry to waste your time :(
    and thanks very much for your help


    jus 1 more thing:
    would u explain this for me plz?:
    Java Code:
    public class Board extends JPanel implements ActionListener {
    
        private Timer timer;
        private Craft craft;
        private ArrayList aliens;
        private boolean ingame;
        private int B_WIDTH;
        private int B_HEIGHT;
    
        private int[][] pos = 
         { 
            {2380, 29}, {2500, 59}, {1380, 89}, ......
         };
    why does it say
    Java Code:
    int [] [] pos ={...}
    shouldnt it be
    Java Code:
    int pos [] [] = {..
    } ?????
    Last edited by bakriawad; 04-16-2013 at 05:43 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help please? Eclipse

    No problem, we all do it.
    That's why getting an extra pair of eyes to look over a problem helps a lot of the time.

    As for your question, you can declare arrays either way.
    I prefer:
    Java Code:
    int[] var;
    over:
    Java Code:
    int var[];
    as I have always read it "int array called var" rather than "int called var which is an array". The latter just makes no sense to me.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Help please? Eclipse

    thx :D
    both make sense to me, and both are useful i guess :D
    since u can do:

    Java Code:
    int[] var1,var2,var3;
    which will make all of them arrays.

    or you can:
    Java Code:
    int var1, var2[], var3;
    if you want to separate :)

    thank you for your help :)

  8. #8
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Help please? Eclipse

    There is still a problem... the missile wont shoot at all... it does not appear what so ever :(
    this code was ready made and i copied and pasted it, so i would learn from it, so i do not have full understanding of it yet.
    Last edited by bakriawad; 04-16-2013 at 07:40 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help please? Eclipse

    I don't see anything in that code that actually paints the missiles.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Help please? Eclipse

    well as i said i do not have full understanding of the code, i am trying to learn how to draw and animate.. so if you can point out where and post a simple code of how to draw it i'd appreciate it

Similar Threads

  1. Replies: 2
    Last Post: 10-11-2013, 09:26 AM
  2. Eclipse Total Beginner Videos for Eclipse 4?
    By Astralogic in forum Eclipse
    Replies: 2
    Last Post: 07-26-2012, 10:14 AM
  3. New Eclipse plugin: shared (and improved) Eclipse Templates
    By dougwightman in forum Java Software
    Replies: 0
    Last Post: 08-10-2011, 05:26 PM
  4. Replies: 0
    Last Post: 08-10-2011, 05:24 PM
  5. Eclipse Bug - Can't Read From A File Using Eclipse?
    By carlodelmundo in forum New To Java
    Replies: 6
    Last Post: 01-26-2009, 04:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •