Results 1 to 20 of 21
Thread: Help Me Please
- 09-27-2013, 09:41 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Help Me Please
This is a simple project i decided to work on got it working writing to a console so i decide to try write it to a JFrame. The Problem is that in line 18 of my drawing class i can not get the string to work i have declared it as public and it won't accept it. i have tried all i can think of so i decided to ask here for help.
Here is my Display class:
Java Code:package Main; import java.awt.Canvas; import java.awt.Graphics; import javax.swing.JFrame; import Main.drawing; public class Display extends Canvas{ public static final int WIDTH = 800; public static final int HEIGHT = 600; public static int ExamMark = 0; public static String Mark; public static void main (String[]args){ Display Window = new Display(); JFrame frame = new JFrame(); frame.add(Window); frame.pack(); frame.setSize(WIDTH, HEIGHT); frame.setResizable(false); frame.setVisible(true); frame.setTitle("Exam Mark Grader Pre-Alpha: 0.2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.add(new drawing ()); if(ExamMark < 50){ System.out.println("Drawed F to screen"); Mark = "F"; }else{ if(ExamMark < 55){ System.out.println("Printed E to screen"); Mark = "E"; }else{ if(ExamMark < 65){ System.out.println("Printed D to screen"); Mark = "D"; }else{ if(ExamMark < 75){ System.out.println("Printed C to screen"); Mark = "C"; }else{ if(ExamMark < 85){ System.out.println("Printed B to screen"); Mark = "B"; }else{ if(ExamMark <= 100){ System.out.println("Printed A to screen"); Mark = "A"; }else{ if(ExamMark > 100){ System.out.println("Printed Invald Mark"); }else{ if(ExamMark < 0){ System.out.println("print Invald Mark"); } } } } } } } } } }
Java Code:package Main; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import Main.Display; public class drawing extends JComponent{ public String Test = "test"; public void paint(Graphics g){ Graphics2D g2d = (Graphics2D) g; g2d.setColor(new Color(124, 23, 179, 255)); g2d.drawString(Mark, 0, 1); } }
- 09-27-2013, 10:11 PM #2
Re: Help Me Please
it won't accept it.If you don't understand my response, don't ignore it, ask a question.
- 09-27-2013, 10:14 PM #3
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
Description Resource Path Location Type
Mark cannot be resolved to a variable drawing.java /ExamMarkGrader/src/Main line 18 Java Problem
i know the mark cannot be resolved to a variable is like when you don't declare it but i did declare it so i am quite confused on this one
- 09-27-2013, 10:31 PM #4
Re: Help Me Please
Where is the variable: Mark defined? Is it in scope where you are trying to use it?
If you made the Drawing class an inner class of Display then the code in Drawing would be able to see the mark variable.
BTW Your class and variable names don't follow the standard: class names start with uppercase, variable names with lowercase.If you don't understand my response, don't ignore it, ask a question.
- 09-27-2013, 10:33 PM #5
Re: Help Me Please
Correct me if I am wrong but you only declared it in Display class. So you have to have Mark brought over to Drawing class with a getter method or something of the sort.
Edit: Oh! I didn't see Norm replied. Yeah, what Norm said.
- 09-27-2013, 10:36 PM #6
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
i will try that and tell you results
- 09-27-2013, 10:39 PM #7
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
i am not sure what you mean sorry.
- 09-27-2013, 10:43 PM #8
Re: Help Me Please
Which solution are you asking about?
See the tutorial for more info: http://docs.oracle.com/javase/tutori...OO/nested.htmlIf you don't understand my response, don't ignore it, ask a question.
- 09-27-2013, 10:46 PM #9
Re: Help Me Please
Put this into Display class
Java Code:public String getMark() { return Mark; }
Java Code:String Mark = Display.getMark();
Last edited by XNOViiCE; 09-27-2013 at 10:49 PM.
- 09-27-2013, 11:00 PM #10
Re: Help Me Please
@XNOViiCE You should test code before posting it to be sure that the code works.
Posting bad code will not help OPs learn Java programming.If you don't understand my response, don't ignore it, ask a question.
- 09-27-2013, 11:15 PM #11
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
thanks that worked but it it had to be static though so i just fixed that so thanks again
- 09-27-2013, 11:20 PM #12
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
well it worked to get rid of the problem anyway but now i get this any idea
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: String is null
at sun.java2d.SunGraphics2D.drawString(Unknown Source)
at Main.drawing.paint(drawing.java:19)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unkn own Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 09-27-2013, 11:20 PM #13
- 09-27-2013, 11:23 PM #14
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
also the updated code:
the Display class:
Java Code:package Main; import java.awt.Canvas; import java.awt.Graphics; import javax.swing.JFrame; import Main.drawing; public class Display extends Canvas{ public static final int WIDTH = 800; public static final int HEIGHT = 600; public static int ExamMark = 0; public static String Mark; public static void main (String[]args){ Display Window = new Display(); JFrame frame = new JFrame(); frame.add(Window); frame.pack(); frame.setSize(WIDTH, HEIGHT); frame.setResizable(false); frame.setVisible(true); frame.setTitle("Exam Mark Grader Pre-Alpha: 0.2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.add(new drawing ()); if(ExamMark < 50){ System.out.println("Drawed F to screen"); Mark = "F"; }else{ if(ExamMark < 55){ System.out.println("Printed E to screen"); Mark = "E"; }else{ if(ExamMark < 65){ System.out.println("Printed D to screen"); Mark = "D"; }else{ if(ExamMark < 75){ System.out.println("Printed C to screen"); Mark = "C"; }else{ if(ExamMark < 85){ System.out.println("Printed B to screen"); Mark = "B"; }else{ if(ExamMark <= 100){ System.out.println("Printed A to screen"); Mark = "A"; }else{ if(ExamMark > 100){ System.out.println("Printed Invald Mark"); }else{ if(ExamMark < 0){ System.out.println("print Invald Mark"); } } } } } } } } } public static String getMark(){ return Mark; } }
Java Code:package Main; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import Main.Display; public class drawing extends JComponent{ String Mark = Display.getMark(); public String Test = "test"; public void paint(Graphics g){ Graphics2D g2d = (Graphics2D) g; g2d.setColor(new Color(124, 23, 179, 255)); g2d.drawString(Mark, 0, 1); } }
- 09-27-2013, 11:24 PM #15
Re: Help Me Please
Initialize Mark and also did you put the getter after your if statements?
- 09-27-2013, 11:26 PM #16
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
what do you mean did i put getter after your statements?
- 09-27-2013, 11:39 PM #17
Re: Help Me Please
After your if statements which I see you have. I also see you have nothing to input the ExamMark. Correct?
- 09-27-2013, 11:53 PM #18
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
yea i do not have that yet after i have sorted this out i am going to start working on the keyboard input for the program
- 09-27-2013, 11:54 PM #19
Member
- Join Date
- Sep 2013
- Posts
- 11
- Rep Power
- 0
Re: Help Me Please
so now that you have seen the code do you have any idea what is causing it?
- 09-28-2013, 12:04 AM #20
Re: Help Me Please
Yeah, what is wrong is that your Mark variable isn't initialized, and since there is no input going to your ExamMark yet it goes to your two bottom ifs and those aren't assigning Mark to anything. This is making the variable null. So you must initialize Mark in your Display class.
In other words do something like this at line 14 of Display class.
Java Code:public static String Mark = "(Enter something here)";
Last edited by XNOViiCE; 09-28-2013 at 12:25 AM.
Bookmarks