Results 1 to 3 of 3
  1. #1
    bozovilla is offline Member
    Join Date
    Nov 2007
    Posts
    19
    Rep Power
    0

    Default Help with Exception

    Hi, my code below gives an error:

    Exception in thread "main" java.lang.NullPointerException
    at Spaceship$DrawA.<init>(Spaceship.java:106)
    at Spaceship.<init>(Spaceship.java:147)
    at Spaceship.main(Spaceship.java:122)

    I am tryingt to create a radar on the west border.


    import java.awt.*;
    import javax.swing.*;

    public class Spaceship extends JFrame implements Runnable{

    private JPanel displayP, radarP, rowsP, speedMeterP, messageP;
    private JLabel display, radar, rows, speedMeter, message;

    Container container;
    Thread t;
    int i=0;
    Font font;


    class Flash extends Thread{
    private DrawA dA;
    private int x;
    private int xCor[];
    private int yCor[];
    private int i;
    private int dir;
    private Image im;
    private Graphics buff;
    public Flash(DrawA dA, Image im, Graphics buff){
    this.dA = dA;
    this.im = im;
    this.buff = buff;
    xCor = new int[451];
    yCor = new int[451];
    dir = 10;
    }//end of Flash constructor
    public void run(){
    for(x = 75, i = 0; x <= 525; x++, i++){
    xCor[i] = x - 300;
    }
    for(i = 0; i <= 450; i++){
    yCor[i] = (int)Math.sqrt(62500 - (xCor[i] * xCor[i]));
    yCor[i] = 300 - yCor[i];
    }

    for(x = 0; x <= 450; x++){
    xCor[x]+=300;
    }

    i = 0;
    while(true){
    try{
    Thread.sleep(35);
    }
    catch(Exception e){}


    dA.repaint();
    i += dir;

    if(i < 0 || i > 450){
    dir = -dir;
    i += dir;
    }//end of if
    }//end of while
    }//end of run
    public void draw(Graphics g){
    int green = 255;
    buff.setColor(Color.black);
    buff.fillRect(0, 0, 600, 330);
    buff.setColor(new Color(0, 70, 0));
    buff.fillArc(50, 50, 500, 500, 25, 130);
    buff.setColor(Color.green);
    if(i > 450)
    return;
    if(xCor[i] != 0 && yCor[i] != 0)
    buff.drawLine(xCor[i], yCor[i], 300, 300);
    if(dir > 0){
    for(int j = i; j > (i - 100); j--){
    if(j >= 0 && j <= 450){
    green -= 2;
    buff.setColor(new Color(0, green, 0));
    buff.drawLine(xCor[j], yCor[j], 300, 300);
    }//end of if
    }//end of for
    }//end of if
    else{
    for(int j = i; j < (i + 100); j++){
    if(j >= 0 && j <= 450){
    green -= 2;
    buff.setColor(new Color(0, green, 0));
    buff.drawLine(xCor[j], yCor[j], 300, 300);
    }// end of if
    }//end of for
    }//end of else
    buff.setColor(Color.green);
    buff.drawArc(112, 112, 375, 375, 25, 130);
    buff.drawArc(174, 174, 250, 250, 25, 130);
    buff.drawArc(238, 238, 125, 125, 25, 130);

    buff.drawLine(300, 300, 300, 50);
    g.drawImage(im, 0, 0, null);
    }//end of draw
    }//end of flash class
    class DrawA extends Panel{
    private Flash fl;
    private Image im;
    private Graphics buff;
    public DrawA(Image im){
    this.im = im;
    buff = im.getGraphics();
    fl = new Flash(this, im, buff);
    fl.start();
    setBackground(Color.black);
    }
    public void paint(Graphics g){
    fl.draw(g);
    }
    public void update(Graphics g){
    paint(g);
    }
    }


    public static void main(String[] args){

    Spaceship f=new Spaceship();
    f.setVisible(true);
    }//end of main

    public DrawA dA;
    public Spaceship()
    {

    setSize(700,500);
    setTitle("Spaceship");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    font=new Font("TimesNewRoman",font.ITALIC,28);

    container = getContentPane();
    container.setLayout(new BorderLayout());

    displayP=new JPanel();
    display = new JLabel("display");
    displayP.add(display);
    container.add(displayP, BorderLayout.NORTH);

    dA = new DrawA(createImage(600, 330));
    container.add(dA, BorderLayout.WEST);

    rowsP =new JPanel();
    rows= new JLabel("rows");
    rowsP.add(rows);
    container.add(rowsP, BorderLayout.CENTER);

    speedMeterP =new JPanel();
    speedMeter= new JLabel("speedmeter");
    speedMeterP.add(speedMeter);
    container.add(speedMeterP, BorderLayout.EAST);

    messageP =new JPanel();
    message= new JLabel();
    messageP.add(message);
    container.add(messageP, BorderLayout.SOUTH);//here messageP is added not counter


    t=new Thread(this);
    t.start();

    }//end of constructor

    public void run()
    {
    for(i=25;i>=0;i--)//countdown
    {
    try
    {
    Thread.sleep(1000);
    message.setText("Estimated Landing Time:"+(Integer.valueOf(i)).toString()+" seconds"); //Here we print the Counter Values
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }

    repaint();
    }//end of run

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem is trying to create a graphics object from a JFrame prior to rendering. For example, run this:
    Java Code:
    import java.awt.Dimension;
    import java.awt.Image;
    
    import javax.swing.JFrame;
    
    public class TestCreateImage
    {
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("TestCreateImage");
        frame.setPreferredSize(new Dimension(300, 300));
        Image img = frame.createImage(600, 300);
        System.out.println("Before JFrame rendered, img == null: " + (img == null));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        img = frame.createImage(600, 300);
        System.out.println("After JFrame rendered, img == null: " + (img == null));
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to provide links in both cross-posts to each other.

Similar Threads

  1. exception
    By thamizhisai in forum Advanced Java
    Replies: 9
    Last Post: 05-30-2008, 09:47 AM
  2. why this exception
    By payal.mitra86 in forum JDBC
    Replies: 1
    Last Post: 05-21-2008, 11:28 PM
  3. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 03:24 PM
  4. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 08:01 PM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 PM

Posting Permissions

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