Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struggling to find source of error 1

Status
Not open for further replies.

raptor1

Technical User
May 12, 2002
2
DE
Hi, I having been struggling to find the ClassCastException Error in the following code which is designed to display a digital number depending on the number entered into the option pane:

Code:
import java.applet.Applet;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;


public class AssignmentTask2 {
	private SDisplay display;


public void init() {

	SDisplay display = new SDisplay(100, 100, 10, 50);

	display.startDisplay();

}

public void paint (Graphics g) {

	display.drawNumber(g);
}
}

class SDisplay extends Applet{

	int num;
	int segmentW;
	int segmentL;
	int Coord [][];
	String Number; 	          // The number entered by the user
	int Segments;	          // The number after conversion from string to integer
        boolean SegmentStatus;

public SDisplay(int width, int length, int x, int y){

	this.segmentW = width;
	this.segmentL = length;
	this.Coord = new int [2] [7];
	this.Coord[0][0] = segmentW + x;
	this.Coord[1][0] = 0 + y;
	this.Coord[0][1] = segmentW + segmentL + x;
	this.Coord[1][1] = segmentW + y;
	this.Coord[0][2] = Coord[0][1];
	this.Coord[1][2] = Coord[1][1] + segmentW + segmentL;
	this.Coord[0][3] = segmentW + x;
	this.Coord[1][3] = (segmentW + segmentL)*2 + y;
	this.Coord[0][4] = 0 + x;
	this.Coord[1][4] = Coord[1][2];
	this.Coord[0][5] = 0 + x;
	this.Coord[1][5] = segmentW + y;
	this.Coord[0][6] = Coord[0][5] + segmentW;
	this.Coord[1][6] = Coord[1][5] + segmentL;
        //this.num = numbers;
}

public void startDisplay(){

	// Read the number from the user

	Number = JOptionPane.showInputDialog (null, "Enter a number between 0 - 9");

	// Convert the string to an integer value

	Segments = Integer.parseInt(Number);
}

public boolean Segment ( int choice, int segmentId ) {

	boolean segmentStatus = false;

	switch (segmentId) {

	case 0 : if ((choice == 0) | (choice == 2) | (choice == 3) | (choice == 5) | (choice == 6)| (choice == 7) | (choice == 8) |
		     (choice == 9)) SegmentStatus = true;
		 else SegmentStatus = false;
		 break;
	case 1 : if ((choice == 0) | (choice == 1) | (choice == 2) | (choice == 3) | (choice == 4)| (choice == 7) | (choice == 8) |
		     (choice == 9)) SegmentStatus = true;
		 else SegmentStatus = false;
		 break;
	case 2 : if ((choice == 0) | (choice == 1) | (choice == 3) | (choice == 4) | (choice == 5)| (choice == 6) | (choice == 7) |
		     (choice == 8) | (choice == 9)) SegmentStatus = true;
		 else SegmentStatus = false;
		 break;
	case 3 : if ((choice == 0) | (choice == 2) | (choice == 3) | (choice == 5) | (choice == 6)| (choice == 8) | (choice == 9)) 		 		 SegmentStatus = true;
		 else SegmentStatus = false;
		 break;
	case 4 : if ((choice == 0) | (choice == 2) | (choice == 6) | (choice == 8))
		 segmentStatus = true;
		 else segmentStatus = false;
		 break;
	case 5 : if ((choice == 0) | (choice == 4) | (choice == 5) | (choice == 6) | (choice == 8)| (choice == 9))
		 segmentStatus = true;
		 else segmentStatus = false;
		 break;
	case 6 : if ((choice == 2) | (choice == 3) | (choice == 4) | (choice == 5) | (choice == 6)| (choice == 8) | (choice == 9))
		 segmentStatus = true;
		 else segmentStatus = false;
		 break;
	}

	return segmentStatus;
}

private void FilledRectangle ( int choice, boolean Seg, Graphics g){

	g.setColor(Color.red);

	if (Seg) {
		g.fillRect(this.Coord[0][choice], this.Coord[0][choice], this.segmentW, this.segmentL);}
	else{
		g.drawRect(this.Coord[0][choice], this.Coord[0][choice], this.segmentW, this.segmentL);
	}
}

private void drawEmpty(int a, boolean v, Graphics g){

        g.setColor(Color.white);

	if (v)
            g.fillRect(this.Coord[0][a], this.Coord[1][a], this.segmentW, this.segmentL);
        else
            g.fillRect(this.Coord[0][a], this.Coord[1][a], this.segmentL, this.segmentW);

}

private void drawSeg (int segments, boolean status, Graphics g){

			if ((status) & ((segments == 1) | (segments == 2) | (segments == 4) | (segments == 5))){
			boolean  t = true;
			FilledRectangle(segments, t, g);
			}
		else if ((status) & ((segments == 0) | (segments == 3) | (segments == 6))){
			boolean t = false;
			FilledRectangle(segments, t, g);
			}
		else if ((!status) & ((segments == 1) | (segments == 2) | (segments == 4) | (segments == 5))){
			boolean t = true;
			drawEmpty(segments, t, g);
			}
		else if ((!status) & ((segments == 0) | (segments == 3) | (segments == 6))){
			boolean t = false;
			drawEmpty(segments, t, g);
			}
}


public void drawNumber(Graphics g) {

	for (int Segments=0; Segments<=6;Segments++){
		boolean state = Segment(this.num, Segments);
		drawSeg ( Segments, state, g);
	}
}
}
 
There is actually a simple answer to your problem. Your class AssignmentTask2 needs to extend from Applet.
 
Thanks for the help wushutwist,

As you can probably tell im rather new to programming in java.

Now its cleared up the ClassCastException error but now theres a null pointer exception occuring.




 
This is another small error in your code. Very common for beginners. In AssignmentTask2.init you are overshadowing your SDisplay object, thus when you attempt to use it in AssignmentTask2.paint a NullPointerException is thrown.

Your code:
Code:
public void init() {
  SDisplay display = new SDisplay(100, 100, 10, 50);
  display.startDisplay();
}
Correct code:
Code:
public void init() {
  display = new SDisplay(100, 100, 10, 50);
  display.startDisplay();
}
Do you see the subtle, but oh so important, difference? Programming is all about the small details.

Disclaimer: This is probably not your only problem.
 
Makes sense. Hmm... I'd better keep that in mind myself.

Note, raptor1, that another similar cause of [tt]NullPointerException[/tt]s is the following:

Incorrect Code:
[tt]
public class DoesntReallyMatter
{
Image buffer = new Image();

public void paint(Graphics g)
{
buffer.drawSomething(...);
}
}
[/tt]

Correct Code:
[tt]
public class DoesntReallyMatter
{
Image buffer = createImage(...);

public void paint(Graphics g)
{
buffer.drawSomething(...);
}
}
[/tt]

As you can see, some types variables have to have content before they can be used, such as draw-on-it able images. That is, only Images that have been [tt]create[/tt]d can be drawn on. Otherwise the Image can only serve as a pointer to an image file. ***
Dammit Jim, I'm a programmer, not a doctor.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top