0

Im trying to create my JFrame like I do always but I don't know why it throws a java.lang.NullpointerException.

I read that it is because you didn;t asign a value, or null can;t be used as a value but I'm almost sure my code is written correctly.

Here it is:

import javax.swing.*;

public class Mathme extends JFrame {
    JFrame home;      
    JLabel title; 

    public Mathme(){
        home.setResizable(false);           !!!13!!!!   
        home.setLocationRelativeTo(null);            
        home.setVisible(true);
        home.setDefaultCloseOperation(EXIT_ON_CLOSE);
        home.setSize(600,500);
        home.setTitle("Pruebax1");
    }    

    public static void main(String[] args) {
        Mathme c = new Mathme(); !!!!23!!!
    }
}

And it throws exactly this:

***Exception in thread "main" java.lang.NullPointerException
    at mathme.Mathme.<init>(Mathme.java:13)
    at mathme.Mathme.main(Mathme.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)***

I will put the number of the line which is referred there.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38

1 Answers1

2

You never initialize the home variable to any object so it makes sense that you'll see a NPE if you try to call methods off of it.

i.e.,

// this declares a variable but doesn't assign anything to it
JFrame home;  // home is not assigned and is currently null

and not

// this both declares a variable and assigns an object reference to it.
JFrame home = new JFrame();

Also why do you have the class extend JFrame and then try to create another one inside the class?

Regarding "beautiful" code -- you will want to learn Java's code formatting rules. You shouldn't indent code randomly but rather all code in a single block should be indented the same amount.

Regarding:

I read that it is because you didnt asign a value, or null cant be used as a value but WTF, I mean Im almost sure my code is written correctly.

This means that you don't yet understand what it means to "assign a value" which is different from declaring a variable (see above) and which is well explained in the first pages of most any intro to Java book. Note that Swing GUI programming is a fairly advanced branch of Java programming, and you will likely be better off first going through some basic tutorials of books before jumping into GUI programming.


This would be another way to create a GUI similar to what you tried to create:

import java.awt.Dimension;
import javax.swing.*;

@SuppressWarnings("serial")
public class MathmePanel  extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 500;

   public MathmePanel() {
      // create my GUI here
   }

   // set size in a flexible and safe way
   @Override
   public Dimension getPreferredSize() {
      Dimension superSize = super.getPreferredSize();
      if (isPreferredSizeSet()) {
         return superSize;
      }
      int prefW = Math.max(PREF_W, superSize.width);
      int prefH = Math.max(PREF_H, superSize.height);
      return new Dimension(prefW, prefH);
   }

   private static void createAndShowGui() {
      MathmePanel mainPanel = new MathmePanel();

      // create JFrame rather than override it
      JFrame frame = new JFrame("Pruebax1");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      // Start Swing GUI in a safe way on the Swing event thread
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373