473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

NullPointerException?

93 64KB
Hello, I have no clue what to do with this error below.

Error:
Exception in thread "main" java.lang.NullPointerException
at main.Universe.<init>(Universe.java:31)
at main.Window.main(Window.java:91)

Window.java:
Expand|Select|Wrap|Line Numbers
  1. package main;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JMenuBar;
  8. import javax.swing.JSlider;
  9. import javax.swing.JToggleButton;
  10.  
  11. public class Window extends JFrame
  12. {
  13.     private static final long serialVersionUID = 1L;
  14.  
  15.     public static int wWidth = 1080;
  16.     public static int wHeight = 720;
  17.     public static boolean wVisible = false;
  18.  
  19.     //Input
  20.     public static int nrMatter = 0;
  21.     public static int nrAMatter = 0;
  22.  
  23.     public Window()
  24.     {
  25.         setTitle("Early universe sim");
  26.         setSize(wWidth, wHeight);
  27.         setVisible(true);
  28.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  29.         getContentPane().setLayout(null);
  30.  
  31.         JMenuBar menuBar = new JMenuBar();
  32.         setJMenuBar(menuBar);
  33.  
  34.         JToggleButton tglbtnRun = new JToggleButton("Run");
  35.         menuBar.add(tglbtnRun);
  36.  
  37.         JSlider slider = new JSlider();
  38.         slider.setMaximum(20);
  39.         slider.setMinimum(10);
  40.         slider.setExtent(1);
  41.         menuBar.add(slider);
  42.     }
  43.  
  44.     public void paint(Graphics g)
  45.     {
  46.         wVisible = true;
  47.         while (wVisible)
  48.         {
  49.             //Check for wall collision before you move any objects
  50.             for (int i = 0; i < Universe.id.length; i++)
  51.             {
  52.                 if (Universe.xPos[i] == 0) { Universe.xSpeed[i] = Universe.xSpeed[i] * -1; }
  53.                 if (Universe.xPos[i] + Universe.cWidth == wWidth) { Universe.xSpeed[i] = Universe.xSpeed[i] * -1; }
  54.                 if (Universe.yPos[i] == 0) { Universe.ySpeed[i] = Universe.ySpeed[i] * -1; }
  55.                 if (Universe.yPos[i] + Universe.cHeight == wHeight) { Universe.ySpeed[i] = Universe.ySpeed[i] * -1; }
  56.             }
  57.  
  58.             //Move
  59.             for (int i = 0; i < Universe.id.length; i++)
  60.             {
  61.                 Universe.xPos[i] = Universe.xPos[i] + Universe.xSpeed[i];
  62.                 Universe.yPos[i] = Universe.yPos[i] + Universe.ySpeed[i];
  63.             }
  64.  
  65.             //Check for Collision here!
  66.  
  67.             //Painting the given amount of Matter at the current position
  68.             for (int i = 0; i < Universe.nrMLoc; i++)
  69.             {
  70.                 g.setColor(Color.RED);
  71.                 g.fillOval(Universe.xPos[i], Universe.yPos[i], Universe.cWidth, Universe.cHeight);
  72.             }
  73.  
  74.             //Painting the given amount of Anti-Matter at the current position
  75.             for(int i = Universe.nrAMLoc; i < Universe.nrAMLoc+Universe.nrMLoc; i++)
  76.             {
  77.                 g.setColor(Color.BLUE);
  78.                 g.fillOval(Universe.xPos[i], Universe.yPos[i], Universe.cWidth, Universe.cHeight);
  79.             }
  80.  
  81.             wVisible = false;
  82.         }
  83.     }
  84.  
  85.     public static void main(String[] args)
  86.     {
  87.         Universe.nrMLoc = 5;
  88.         Universe.nrAMLoc = 5;
  89.  
  90.         //Initializing the early universes content
  91.         Universe u = new Universe();
  92.  
  93.         //Construct window
  94.         Window w = new Window();
  95.     }
  96. }
  97.  
Universe.java:
Expand|Select|Wrap|Line Numbers
  1. package main;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Universe 
  6. {
  7.     //Input
  8.     public static int nrMLoc = 1;
  9.     public static int nrAMLoc = 1;
  10.     //Universe properties
  11.     public static int cWidth = 25;
  12.     public static int cHeight = 25;
  13.     public static int maxX = Window.wWidth;
  14.     public static int minX = 0;
  15.     public static int maxY = Window.wHeight;
  16.     public static int minY = 0;
  17.     //Physics
  18.     public static int gravity = 10;
  19.     //Matter and Anti-Matter properties
  20.     public static int[] id;
  21.     public static int[] xPos;
  22.     public static int[] yPos;
  23.     public static int[] xSpeed;
  24.     public static int[] ySpeed;
  25.  
  26.     public Universe()
  27.     {
  28.         Random r = new Random();
  29.         for (int i = 0; i < nrMLoc+nrAMLoc; i++)
  30.         {
  31.             id[i] = i;
  32.             xPos[i] = r.nextInt(Window.wWidth-2*cWidth)+cWidth;
  33.             yPos[i] = r.nextInt(Window.wHeight-2*cHeight)+cHeight;
  34.             xSpeed[i] = r.nextInt(30);
  35.             ySpeed[i] = r.nextInt(30);
  36.         }
  37.     }
  38. }
  39.  
Dec 16 '15 #1
1 1465
Xillez
93 64KB
Found it out. java is trying to convert from the data type null to int, and that is not possible. Just add integers in the array when you declare it.
Dec 16 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of...
4
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
0
by: Old-timer | last post by:
Not sure where else to post this. I'm sure I'm doing something wrong, but I wouldn't think a simple app would give me so much trouble. I've got a small test java class that I'm trying to have...
2
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
13
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
1
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.