Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9044879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:05:59+00:00 2026-06-16T11:05:59+00:00

I faced a null pointer exception when I tried to retrieve an object from

  • 0

I faced a null pointer exception when I tried to retrieve an object from a normal array. First I initialize the array with 10 Line objects and then set the values of each object inside the array. But when I retrieve the values of any objects, I found it to be 0’s. Why does that happen?

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class LineArrayLimt extends Applet
    implements MouseListener, MouseMotionListener {

    int width, height;
    int x, y, lineCounter;    // the coordinates of the upper-left corner of the box
    int mx, my;  // the most recently recorded mouse coordinates
    int new_mx;
    int new_my;
    Thread th;
    boolean isMouseDragging = false, isStored;
    Line[] lines = new Line[10];

    class Line {

        Line() {
        }

        Line(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
        private int x1, y1, x2, y2;

        public void setX1(int x) {
            this.x1 = x;
        }

        public void setY1(int y) {
            this.y1 = y;
        }

        public void setX2(int x) {
            this.x2 = x;
        }

        public void setY2(int y) {
            this.y2 = y;
        }

        public int getX1() {
            return x1;
        }

        public int getY1() {
            return y1;
        }

        public int getX2() {
            return x2;
        }

        public int getY2() {
            return y2;
        }
    }

    public void init() {
        width = getSize().width;
        height = getSize().height;
        setBackground(Color.black);
        addMouseListener(this);
        addMouseMotionListener(this);
        for (int i = 0; i < lines.length; i++) {
            lines[i] = new Line();
        }

    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        System.out.println("-----------------Mouse Pressed------------------------");
        mx = e.getX();
        my = e.getY();



    }

    public void mouseReleased(MouseEvent e) {
        System.out.println("-----------------Mouse Released------------------------");
        if (isMouseDragging) {
            if (lineCounter != lines.length - 1) {
                lines[lineCounter].setX1(mx);
                lines[lineCounter].setY1(my);
                lines[lineCounter].setX2(new_mx);
                lines[lineCounter].setY2(new_my);
                lineCounter++;
                if (lines[lineCounter] != null) {
                    System.out.println("-----------------First Object------------------------" + lines[lineCounter].getX1() + " " + lines[lineCounter].getY1() + " " + lines[lineCounter].getX2() + " " + lines[lineCounter].getY1());
                }


            }

        }
        isMouseDragging = false;

    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        System.out.println("-----------------Mouse Dragged------------------------");
        isMouseDragging = true;
        new_mx = e.getX();
        new_my = e.getY();
        if (new_mx <= 35) {
            new_mx = 35;
        }
        if (new_mx > width - 40) {
            new_mx = width - 40;
        }
        if (new_my > height - 40) {
            new_my = height - 40;
        }
        if (new_my < 35) {
            new_my = 35;
        }
        repaint();

    }

    public void paint(Graphics g) {
        System.out.println("-----------------Line No." + lineCounter + " is inside paint------------------------");
        g.setColor(Color.RED);
        if (isMouseDragging) {
            System.out.println("-----------------Paint while dragging------------------------");
            g.drawLine(mx, my, new_mx, new_my);
            if (lineCounter != lines.length - 1) {
                //if(lines[lineCounter]!=null){
                g.drawLine(lines[lineCounter].getX1(), lines[lineCounter].getY1(), lines[lineCounter].getX2(), lines[lineCounter].getX2());
                System.out.println("*************" + lines[lineCounter].getX1() + lines[lineCounter].getY1() + lines[lineCounter].getX2() + lines[lineCounter].getX2());
                //}
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:06:00+00:00Added an answer on June 16, 2026 at 11:06 am

    The problem is that in init you’re initializing lines to an array of 10 null references:

    lines = new Line[10];
    

    Then in paint you’re using that null reference:

    if (lineCounter != lines.length - 1) {
        g.drawLine(lines[lineCounter].getX1(),
                   lines[lineCounter].getY1(),
                   lines[lineCounter].getX2(),
                   lines[lineCounter].getY2());
    }
    

    lines.length will always be 10 – it isn’t the number of non-null references in the array. It’s not the number of lines you’ve added, or anything like that. (It’s not clear what your if condition is trying to achieve, to be honest.)

    You’d be better off using a List<Line> instead. Then in paint you could just use:

    for (Line line : lines) {
        ...
    }
    

    … and in mouseReleased you’d just use:

    lines.add(new Line(...));
    

    Also note that there’s no reason for Line to be an inner class. Either make it a static nested class, or a top-level (non-nested) class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a null pointer exception when run. I am supposed to create
HI i am getting null pointer exception while sending image path to another activity
Below is the code I am trying to test, but getting null pointer exception
I'm getting a Null Pointer Exception when using my web app. This exception is
I faced a problem while using threading for the first time, In an SWT
After migrating JSF from 1.2 to 2.0 I begin to receive exception after submitting
Here's the problem: The EJB throws this exception (from the glassfish logs): SEVERE: Attempting
I try to write first maven web app with jsf. And I faced with
I faced SSLHandshakeException on Heroku. This app was not SSL app. But this app
I faced following issue with my work: having two projects: MFC .dll and MFC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.