-2

I've tried going to the original Null Pointer Exception question, but it didn't help. Here is the first and second line of the stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Client.SpriteField.paintComponent(SpriteField.java:30)

So this is telling me that it is in the package "Client", the class "SpriteField", and the method "paintComponent" right? Here is the code in the class SpriteField:

package Client;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JPanel;

public class SpriteField extends JPanel 
{
    RoundSprite mSprite;
    public void CreateSpriteAt(float tX, float tY, int tWidth, int tHeight, int tRotation) 
    {
        mSprite = new RoundSprite();
        mSprite.SetPosition(tX, tY);
        mSprite.SetSpriteWidth(tWidth);
        mSprite.SetSpriteHeight(tHeight);
        mSprite.SetSpriteRotation(tRotation);
    }
    public void paintComponent (Graphics g)
    {
        if(g != null)
        {
            super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        AffineTransform tOldTransform = g2.getTransform();

        mSprite.DrawSprite(g2);

        g2.setTransform(tOldTransform);
        }
    }

}

It might have been just a careless mistake, but I can't seem to find it. Please help, thanks for your time.

EDIT: As asked, here is the main method:

final SpriteField mSpritePanel = new SpriteField();
mSpritePanel.addMouseListener(new MouseAdapter() 
{
    public void mouseClicked(MouseEvent e)
    {
        float tX = e.getX();
        float tY = e.getY();
        if(tTextWidth.getText() == null)
        {
            tTextWidth.setText("50");
        }
        int tIntWidth = Integer.parseInt(tTextWidth.getText());
        if(tIntWidth == 0)
        {
            tIntWidth = 50;
        }
        if(tTextHeight.getText() == null)
        {
            tTextHeight.setText("50");
        }
        int tIntHeight = Integer.parseInt(tTextHeight.getText());
        if(tIntHeight == 0)
        {
            tIntHeight = 50;
        }
        int tRotate = Integer.parseInt(tTextRotation.getText());
        mSpritePanel.CreateSpriteAt(tX, tY, tIntWidth, tIntHeight, tRotate); 
        mSpritePanel.repaint();
    }
});
Community
  • 1
  • 1
okamiaaron
  • 90
  • 1
  • 8

2 Answers2

4

Here is the issue

mSprite.DrawSprite(g2); // you need to initialize mSprite

But here mSprite is null.

You can find this kind of mistakes easily by debugging.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

You are not calling CreateSpriteAt until much later, after the component is already on screen. The first time it gets to mSprite.DrawSprite(g2); it will of course blow up with an NPE because the mSprite field is initially null.

The simple fix is to not try to draw the sprite when the variable is null:

if (mSprite != null) mSprite.DrawSprite(g2);

However, since you have named this class SpriteField, perhaps a sprite list is what you really want, rather than a single sprite?

public class SpriteField extends JPanel {
    final List<RoundSprite> sprites = new ArrayList<>();

    public void CreateSpriteAt(float tX, float tY, int tWidth, int tHeight, int tRotation) {
        RoundSprite s = new RoundSprite();
        s.SetPosition(tX, tY);
        s.SetSpriteWidth(tWidth);
        s.SetSpriteHeight(tHeight);
        s.SetSpriteRotation(tRotation);
        sprites.add(s);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        AffineTransform originalTransform = g2.getTransform();

        for (RoundSprite s : sprites) {
            s.DrawSprite(g2);
            g2.setTransform(originalTransform);
        }
    }
}

P.S. Please consider following standard Java naming conventions!

Boann
  • 48,794
  • 16
  • 117
  • 146