Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Help Me Please

  1. #1
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Question 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");
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    		
    		
    			
    		
    		
    		}
    	}
    And here is my drawing class:
    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);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Help Me Please

    it won't accept it.
    That sounds like there could be an error message. Please copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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.

  5. #5
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default 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.

  6. #6
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help Me Please

    i will try that and tell you results

  7. #7
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help Me Please

    i am not sure what you mean sorry.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Help Me Please

    Which solution are you asking about?

    See the tutorial for more info: http://docs.oracle.com/javase/tutori...OO/nested.html
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Help Me Please

    Put this into Display class
    Java Code:
     public String getMark()
       {
          return Mark;
       }
    then in drawing class put this in!
    Java Code:
     String Mark = Display.getMark();
    Pretty sure I haven't messed that up. lol
    Last edited by XNOViiCE; 09-27-2013 at 09:49 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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.

  11. #11
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help Me Please

    thanks that worked but it it had to be static though so i just fixed that so thanks again

  12. #12
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default 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)

  13. #13
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Help Me Please

    Quote Originally Posted by Norm View Post
    @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.
    Yeah, I usually do but his program was giving me problems when I tried to recreate it. :( I referenced my other programs to make sure I was correct which I am pretty sure is right but I cannot be %100. I do usually test it out though.

  14. #14
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default 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;		
    		}
    	}
    And the Drawing Class:
    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);
    	}
    }
    Thanks

  15. #15
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Help Me Please

    Initialize Mark and also did you put the getter after your if statements?

  16. #16
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help Me Please

    what do you mean did i put getter after your statements?

  17. #17
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Help Me Please

    After your if statements which I see you have. I also see you have nothing to input the ExamMark. Correct?

  18. #18
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default 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

  19. #19
    outlaw1198 is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help Me Please

    so now that you have seen the code do you have any idea what is causing it?

  20. #20
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default 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-27-2013 at 11:25 PM.

Page 1 of 2 12 LastLast

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
  •