4 Replies - 6914 Views - Last Post: 17 September 2008 - 05:57 PM Rate Topic: -----

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Simple Java GUI

Posted 17 September 2008 - 04:08 PM

This is sad I can't get it to work, but each and every time I try to compile the code it shows this error:

Exception in thread "main" java.lang.NullPointerException
	at java.awt.window.init(Unknown Source)
	at java.awt.window.<init>(Unknown Source)
	at java.awt.Frame.<init>(Unknown Source)
	at java.awt.Frame.<init>(Unknown Source)
	at javax.swing.JFrame.<init>(Unknown Source)
	at Runner.main(Runner.java:7)


Here is my code:

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

public class Runner{
	public static void main(String[] args){
		JFrame gui = new JFrame();
		gui.setTitle("Test");
		gui.setSize(300, 200);
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ColorPanel panel = new ColorPanel(Color.white);
		Container pane = gui.getContentPane();
		pane.add(panel);
		
		gui.setVisible(true);
		
	}
}


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

public class ColorPanel extends JPanel{
	private Rectangle r1;
	private Rectangle r2;
	public ColorPanel(Color backColor){
		setBackground(backColor);
		r1 = new Rectangle(200, 100, 25, 30, Color.red);
		r2 = new Rectangle(100, 100, 50, 50, Color.blue);
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		r1.fill(g);
		r2.draw(g);
	}
}


Rectangle.java:
import java.awt.*;

public class Rectangle{
	private int x;
	private int y;
	private int width;
	private int height;
	private Color color;
	public Rectangle(int nx, int ny, int nwidth, int nheight, Color c){
		x = nx;
		y = ny;
		width = nwidth;
		height = nheight;
		color = c;
	}
	public void draw(Graphics g){
		Color oldColor = g.getColor();
		g.setColor(color);
		g.drawRect(x, y, width, height);
		g.setColor(oldColor);
	}
	public void fill(Graphics g){
		Color oldColor = g.getColor();
		g.setColor(color);
		g.fillRect(x, y, width, height);
		g.setColor(oldColor);
	}
	public boolean containsPoint(int px, int py){
		if(px >= x && px <= (x+width) && py >= y && py <= (py+height)){
			return true;
		}
		return false;
	}
	public void move(int xAmt, int yAmt){
		x += xAmt;
		y += yAmt;
	}
}


Now, I can't figure out why this code won't work. I am fairly new to Java GUI programming, but I don't see why it has an error.


The following code and it DOES work:

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

public class WindowNew{
	public static void main(String[] args){
		JFrame gui = new JFrame();
		gui.setTitle("Test");
		gui.setSize(300, 200);
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//ColorPanel panel = new ColorPanel(Color.white);
		Arrows panel2 = new Arrows(Color.white);
		Container pane = gui.getContentPane();
		//pane.add(panel);
		pane.add(panel2);
		
		gui.setVisible(true);
		
	}
}



Any help would be nice.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Java GUI

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Simple Java GUI

Posted 17 September 2008 - 04:31 PM

I cut & paste iall your code and ran it without problem

Your error (when you try to run it NOT when to try to compile it) is at line 7 of Runner which is

gui.setTitle("Test");

no reason to crash there

You sure all your file are in the same directory ? And you did javac *.java ?

java.awt. also have a class Rectangle but I don't thing it can be related to your problem
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Simple Java GUI

Posted 17 September 2008 - 04:45 PM

This line is the one I keep getting errors on:
JFrame gui = new JFrame();

(I must have gotten rid of a line when I posted the original code, but, looking at it again, the whole code is there, so I must have posted it wrong)...


All the files are saved in the same folder so I don't think that is the problem...
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Simple Java GUI

Posted 17 September 2008 - 05:26 PM

View PostBetaWar, on 17 Sep, 2008 - 04:45 PM, said:

This line is the one I keep getting errors on:
JFrame gui = new JFrame();

(I must have gotten rid of a line when I posted the original code, but, looking at it again, the whole code is there, so I must have posted it wrong)...


All the files are saved in the same folder so I don't think that is the problem...


Works well on my XP with JRE 1.5.x
Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Simple Java GUI

Posted 17 September 2008 - 05:57 PM

Well, I don't know what is wrong with my other computer, I copied it over (exaclty) and it works now... Thanks for the help anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1