Page 1 of 2 12 LastLast
Results 1 to 20 of 29
Like Tree5Likes

Thread: Null Pointer Exception in awt interface

  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Null Pointer Exception in awt interface

    I created the awt -swing interface for drawing the Line. I ahave 6 inputfiled for 4 coordinates of start and end point, for colour and sickness. When I left empty the coour filed it did not call exception during the run, once when empty coordinate and thickness filed it throws Numberformatexception.
    But the probem is when i f.e. put such vaues in coordinates-3,3;4.4 and 5 for thickness it throw nullpoiner exception during the run --simply the -- "java.lang.NullPointerException" without any reference. So what is the reason not corect format of x and y coordinates or sickness, or empty colour filed (what I should enter-BLUE. Blue, some number?).
    Here is the code for the check for exception:
    public void actionPerformed(ActionEvent arg0) {
    try {
    line = new Line(mainView.getFirstPointXCoordinate(),
    mainView.getFirstPointYCoordinate(),
    mainView.getSecondPointXCoordinate(),
    mainView.getSecondPointYCoordinate());

    try {
    Graphics g =null;

    mainView.setVisible(false);
    lineView.setVisible(true);

    lineView.paintComponent(g, line, mainView.getLineColor(),
    (float)mainView.getLineThickness());


    } catch (NullPointerException ex) {
    System.out.println(ex);
    }

    } catch (NumberFormatException e) {
    System.out.println(e);

    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Null Pointer Exception in awt interface

    Quote Originally Posted by ark View Post
    Java Code:
    ...
    					Graphics g =null;
    
    					mainView.setVisible(false);
    					lineView.setVisible(true);
    
    					lineView.paintComponent(g, line, mainView.getLineColor(),
    ...
    You set a Graphics object g to null; how is your lineView supposed to draw anything at all in the last line?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Null Pointer Exception in awt interface

    You should print out the exceptions like this:

    Java Code:
    ex.printStackTrace();
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    at javax.swing.JComponent.paintComponent(JComponent.j ava:766)
    at home.java.view.LineView.paintComponent(LineView.ja va:23)
    at home.java.controller.LineController$ButtonListener .actionPerformed(LineController.java:35)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6041)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
    at java.awt.Component.processEvent(Component.java:580 6)
    at java.awt.Container.processEvent(Container.java:205 8)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4413)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
    at java.awt.Component.dispatchEvent(Component.java:42 43)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2440 )
    at java.awt.Component.dispatchEvent(Component.java:42 43)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
    ---Or here are a very big list of errors it is difficult even where to search?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Null Pointer Exception in awt interface

    You're ignoring JosAH's response.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Null Pointer Exception in awt interface

    In a big stack trace like that the first step is to find the first mention of your code:
    at home.java.controller.LineController$ButtonListener .actionPerformed(LineController.java:35)

    That's usually a good spot to look for the offending item.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    I suggest that the error in it (If it so). But How to set Graphics g correctly. What is the options as the code is in
    several classes (files) so it difficult to refer it here?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Null Pointer Exception in awt interface

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    LineView23:
    @Override
    public Rectangle2D getBounds2D() {
    return null;
    } ---So I need correctly override this method. as simply delete it cause another error about that it is abstract method.

    LineController35:
    lineView.paintComponent(g, line, mainView.getLineColor(),(float)mainView.getLineThi ckness()); -- ?
    And here is the description of paintComponent in LineView class:
    public void paintComponent(Graphics g, Object object, String color,
    float thickness) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(new BasicStroke(thickness));
    g2.setColor(Color.getColor(color));
    g2.draw((Shape) object);

    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Null Pointer Exception in awt interface

    Normally (which means 99.9 percent of cases), you don't call paintComponent yourself. You may override it as necessary. But it is called when you issue a repaint(). That presumes that it is a child of a paintable object like JPanel or JFrame. JFrame.repaint() will cause it to repaint itself including its descendents. Did you read the link posted by Darryl?

    Regards,
    Jim
    Last edited by jim829; 04-15-2015 at 06:13 PM. Reason: grammar
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    here is the whole class: And it is said in the NET that paintComponent(Graphics g) is enough and should not be overrideen. But anyway Graphics g is initialized to NULL so it is impossible to use it. As well as getBounds()--I could not find any example of oberriding of this method and what it is exactly? The simple rectangle that include left-upper and down-right edges of ile ofr instance or the precise outline of this shape (the line in that case).

    public class LineView extends JPanel {
    public LineView() {
    this.setSize(600, 400);
    this.setLayout(new GridBagLayout());
    }
    public void paintComponent(Graphics g, Object object, String color,
    float thickness) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(new BasicStroke(thickness));
    g2.setColor(Color.getColor(color));
    g2.draw((Shape) object); }

    Here is the:
    public class LineController {
    private Line line;
    private MainView mainView;
    private LineView lineView;
    public LineController(Line line, MainView mainView, LineView lineView) {

    this.line = line;
    this.mainView = mainView;
    this.lineView = lineView;
    this.mainView.addButtonListener(new ButtonListener());

    }
    class ButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
    try {
    line = new Line(mainView.getFirstPointXCoordinate(),
    mainView.getFirstPointYCoordinate(),
    mainView.getSecondPointXCoordinate(),
    mainView.getSecondPointYCoordinate());

    try {
    Graphics g =null;

    mainView.setVisible(false);
    lineView.setVisible(true);

    lineView.paintComponent(g, line, mainView.getLineColor(),
    (float)mainView.getLineThickness());


    } catch (NullPointerException ex) {
    ex.printStackTrace();
    }

    } catch (NumberFormatException e) {
    System.out.println(e);

    }
    }
    }
    }

  12. #12
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    Maybe it is not correct to acces from another class: mainView.getFirstPointYCoordinate().
    Maybe the paccages should be in the same folder ut not that:
    package home.java.controller(for linecontroller);
    package home.java.view(for lineview);

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Null Pointer Exception in awt interface

    Quote Originally Posted by ark View Post
    here is the whole class: And it is said in the NET that paintComponent(Graphics g) is enough and should not be overrideen.
    I think you misunderstood what you read.
    You override paintComponent(), yes...but you call repaint(), never paintComponent() directly. That's Swings job, not yours.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    So here:
    try {
    Graphics g = null;
    mainView.setVisible(false);
    lineView.setVisible(true);
    lineView.paintComponent(g, line, mainView.getLineColor(),
    (float) mainView.getLineThickness()); // I should use repaint--??
    } catch (NullPointerException ex) {
    System.out.println(ex);
    }

  15. #15
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    No it throw exception--cannot find symbol??

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Null Pointer Exception in awt interface

    And?
    What's the full error message?

    What does your code look like now?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Null Pointer Exception in awt interface

    Go through the tutorial. Do the exercises.

    At present, this is where you're headed:
    Null Pointer Exception in awt interface-headdesk.gif

    db
    jim829, SurfMan and gimbal2 like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    symbol : method repaint(java.awt.Graphics,home.java.model.Line,jav a.lang.String,float)
    location: class home.java.view.LineView
    lineView.repaint(g, line, mainView.getLineColor()------
    try {
    Graphics g =null;

    mainView.setVisible(false);
    lineView.setVisible(true);

    lineView.paintComponent(g, line, mainView.getLineColor(),
    (float)mainView.getLineThickness());


    } catch (NullPointerException ex) {
    ex.printStackTrace();
    }

    } catch (NumberFormatException e) {
    System.out.println(e);

    } catch (NumberFormatException e) {
    System.out.println(e);
    }

  19. #19
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Null Pointer Exception in awt interface

    To be frankly now I want to make the simpler code, that is already ready--it launch the interface--for the input (but maybe I do not correctly define the getcolour--what should I input). Maybe, I do not need to override Line2D but just to use Line2D.Double.
    So my task is to use clases (here is 2 classes of Point and Line), views (Lineview and MainView), Controller (LineController) and MainApll.
    //But I have also to make the amp in Java, and today I saw that using the multiple lines in Java is not easy task so I want to show to teacher at least the simper task.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Null Pointer Exception in awt interface

    You're ignoring gimbal2's response (reply #5).

    kind regards,

    Jos
    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

Page 1 of 2 12 LastLast

Similar Threads

  1. null pointer exception
    By raymasuku in forum Enterprise JavaBeans (EJB)
    Replies: 6
    Last Post: 11-28-2013, 10:57 AM
  2. Null Pointer Exception
    By jameshl2008 in forum New To Java
    Replies: 6
    Last Post: 10-22-2013, 02:56 PM
  3. Why I am getting null pointer exception?
    By ashvini in forum New To Java
    Replies: 6
    Last Post: 04-03-2013, 06:02 PM
  4. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 07:16 AM
  5. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 05:04 PM

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
  •