Hello, I have no clue what to do with this error below.
Error:
Exception in thread "main" java.lang.NullP ointerException
at main.Universe.< init>(Universe. java:31)
at main.Window.mai n(Window.java:9 1)
Window.java:
Universe.java:
Error:
Exception in thread "main" java.lang.NullP ointerException
at main.Universe.< init>(Universe. java:31)
at main.Window.mai n(Window.java:9 1)
Window.java:
Code:
package main;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
public class Window extends JFrame
{
private static final long serialVersionUID = 1L;
public static int wWidth = 1080;
public static int wHeight = 720;
public static boolean wVisible = false;
//Input
public static int nrMatter = 0;
public static int nrAMatter = 0;
public Window()
{
setTitle("Early universe sim");
setSize(wWidth, wHeight);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JToggleButton tglbtnRun = new JToggleButton("Run");
menuBar.add(tglbtnRun);
JSlider slider = new JSlider();
slider.setMaximum(20);
slider.setMinimum(10);
slider.setExtent(1);
menuBar.add(slider);
}
public void paint(Graphics g)
{
wVisible = true;
while (wVisible)
{
//Check for wall collision before you move any objects
for (int i = 0; i < Universe.id.length; i++)
{
if (Universe.xPos[i] == 0) { Universe.xSpeed[i] = Universe.xSpeed[i] * -1; }
if (Universe.xPos[i] + Universe.cWidth == wWidth) { Universe.xSpeed[i] = Universe.xSpeed[i] * -1; }
if (Universe.yPos[i] == 0) { Universe.ySpeed[i] = Universe.ySpeed[i] * -1; }
if (Universe.yPos[i] + Universe.cHeight == wHeight) { Universe.ySpeed[i] = Universe.ySpeed[i] * -1; }
}
//Move
for (int i = 0; i < Universe.id.length; i++)
{
Universe.xPos[i] = Universe.xPos[i] + Universe.xSpeed[i];
Universe.yPos[i] = Universe.yPos[i] + Universe.ySpeed[i];
}
//Check for Collision here!
//Painting the given amount of Matter at the current position
for (int i = 0; i < Universe.nrMLoc; i++)
{
g.setColor(Color.RED);
g.fillOval(Universe.xPos[i], Universe.yPos[i], Universe.cWidth, Universe.cHeight);
}
//Painting the given amount of Anti-Matter at the current position
for(int i = Universe.nrAMLoc; i < Universe.nrAMLoc+Universe.nrMLoc; i++)
{
g.setColor(Color.BLUE);
g.fillOval(Universe.xPos[i], Universe.yPos[i], Universe.cWidth, Universe.cHeight);
}
wVisible = false;
}
}
public static void main(String[] args)
{
Universe.nrMLoc = 5;
Universe.nrAMLoc = 5;
//Initializing the early universes content
Universe u = new Universe();
//Construct window
Window w = new Window();
}
}
Code:
package main;
import java.util.Random;
public class Universe
{
//Input
public static int nrMLoc = 1;
public static int nrAMLoc = 1;
//Universe properties
public static int cWidth = 25;
public static int cHeight = 25;
public static int maxX = Window.wWidth;
public static int minX = 0;
public static int maxY = Window.wHeight;
public static int minY = 0;
//Physics
public static int gravity = 10;
//Matter and Anti-Matter properties
public static int[] id;
public static int[] xPos;
public static int[] yPos;
public static int[] xSpeed;
public static int[] ySpeed;
public Universe()
{
Random r = new Random();
for (int i = 0; i < nrMLoc+nrAMLoc; i++)
{
id[i] = i;
xPos[i] = r.nextInt(Window.wWidth-2*cWidth)+cWidth;
yPos[i] = r.nextInt(Window.wHeight-2*cHeight)+cHeight;
xSpeed[i] = r.nextInt(30);
ySpeed[i] = r.nextInt(30);
}
}
}
Comment