This question already has an answer here:
I'm having a problem with my java project. i'm a beginner and I can't seem to find the error. The full error code is this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ProgramState.PeriodicTableState.mousePressed(PeriodicTableState.java:423)
at ProgramState.ProgramStateManager.mousePressed(ProgramStateManager.java:66)
at Main.ProgramPanel.mousePressed(ProgramPanel.java:122)
at java.awt.Component.processMouseEvent(Component.java:6530)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
I didn't send all of it because it is too long. My class is this:
package ProgramState;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import Background.Background;
import Entity.Element;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
public class PeriodicTableState extends ProgramState{
private Background bg;
private Element hydrogen;
private Element helium;
private Element lithium;
private Element beryllium;
private Element boron;
private Element carbon;
private Element nitrogen;
private Element oxygen;
private Element fluorine;
private Element neon;
private Element sodium;
private Element magnesium;
private Element aluminium;
private Element phosphorus;
private Element silicon;
private Element chlorine;
private Element argon;
private Element potassium;
private Element calcium;
private Element scandium;
private Element titanium;
private Element vanadium;
private Element chromium;
private Element manganese;
private Element iron;
private Element cobalt;
private Element nickel;
private Element copper;
private Element zinc;
private Element gallium;
private Element germanium;
private Element arsenic;
private Element selenium;
private Element bromine;
private Element krypton;
private Element rubidium;
private Element strontium;
private Element yttrium;
private Element zirconium;
private Element niobium;
private Element molybdenum;
private Element technetium;
private Element ruthenium;
private Element rhodium;
private Element palladium;
private Element silver;
private Element cadmium;
private Element indium;
private Element tin;
private Element antimony;
private Element tellurium;
private Element iodine;
private Element xenon;
private Element cesium;
private Element barium;
private Element lanthanum;
private Element cerium;
private Element praseodymium;
private Element neodymium;
private Element promethium;
private Element samarium;
private Element europium;
private Element gadolinium;
private Element terbium;
private Element dysprosium;
private Element holmium;
private Element erbium;
private Element thulium;
private Element ytterbium;
private Element lutetium;
private Element hafnium;
private Element tantalum;
private Element tungsten;
private Element rhenium;
private Element osmium;
private Element iridium;
private Element platinum;
private Element gold;
private Element mercury;
private Element thallium;
private Element lead;
private Element bismuth;
private Element polonium;
private Element astatine;
private Element radon;
private Element francium;
private Element radium;
private Element actinium;
private Element thorium;
private Element protactinium;
private Element uranium;
private Element neptunium;
private Element plutonium;
private Element americium;
private Element curium;
private Element berkelium;
private Element californium;
private Element einsteinium;
private Element fermium;
private Element mendelevium;
private Element nobelium;
private Element lawrencium;
private Element rutherfordium;
private Element dubnium;
private Element seaborgium;
private Element bohrium;
private Element hassium;
private Element meitnerium;
private Element darmstadtium;
private Element roentgenium;
private Element copernicium;
private Element nihonium;
private Element flerovium;
private Element moscovium;
private Element livermorium;
private Element tennessine;
private Element oganession;
private Element[] elements;
public PeriodicTableState(ProgramStateManager psm) {
this.psm = psm;
try {
bg = new Background("/Background/PeriodicTableTest.png", 1);
bg.setVector(0, 0);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void init() {
Element hydrogen = new Element("hydrogen", 89, 39);
Element helium = new Element("helium", 1056, 39);
Element lithium = new Element("lithium", 89, 97);
Element beryllium = new Element("beryllium", 146, 97);
Element boron = new Element("boron", 771, 97);
Element carbon = new Element("carbon", 829, 97);
Element nitrogen = new Element("nitrogen", 886,97);
Element oxygen = new Element("oxygen", 924,97);
Element fluorine = new Element("fluorine", 1000,97 );
Element neon = new Element("neon", 1056,97);
Element sodium = new Element("sodium", 89, 155);
Element magnesium = new Element("magnesium", 146, 155);
Element aluminum = new Element("aluminum", 771,155);
Element phosphorus = new Element("phosphorus",886,155);
Element silicon = new Element("silicon", 829,155);
Element sulfur = new Element("sulfur", 942,155);
Element chlorine = new Element("chlorine", 1000,155);
Element argon = new Element("argon",1056,155 );
Element potassium = new Element("potassium", 89, 213);
Element calcium = new Element("calcium", 146, 213);
Element scandium = new Element("scandium", 203,213);
Element titanium = new Element("titanium", 260,213);
Element vanadium = new Element("vanadium",317,213);
Element chromium = new Element("chromium", 374,213);
Element manganese = new Element("manganese", 431,213);
Element iron = new Element("iron", 488,213);
Element cobalt = new Element("cobalt", 544,213);
Element nickel = new Element("nickel", 601,213);
Element copper = new Element("copper", 657,213);
Element zinc = new Element("zinc", 715,213);
Element gallium = new Element("gallium", 771,213);
Element germanium = new Element("germanium", 829,213);
Element arsenic = new Element("arsenic", 886,213);
Element selenium = new Element("selenium", 942,213);
Element bromine = new Element("bromine", 1000,213);
Element krypton = new Element("krypton", 1054,213);
Element rubidium = new Element("rubidium", 89,271);
Element strontium = new Element("strontium", 146,271);
Element yttrium = new Element("yttrium", 203,271);
Element zirconium = new Element("zirconium", 260,271);
Element niobium = new Element("niobium", 317,271);
Element molybdenum = new Element("molybdenum", 374,271);
Element technetium = new Element("technetium", 431,271);
Element ruthenium = new Element("ruthenium", 488,271);
Element rhodium = new Element("rhodium", 544,271);
Element palladium = new Element("palladium", 601,271);
Element silver = new Element("silver", 657,271);
Element cadmium = new Element("cadmium", 715,271);
Element indium = new Element("indium", 771,271);
Element tin = new Element("tin", 829,271);
Element antimony = new Element("antimony", 886,271);
Element tellurium = new Element("tellurium", 942,271);
Element iodine = new Element("iodine", 1000,271);
Element xenon = new Element("xenon", 1056,271);
Element cesium = new Element("cesium", 89,330);
Element barium = new Element("barium", 146,330);
Element lanthanum = new Element("lanthanum", 231,460);
Element cerium = new Element("cerium", 288,460);
Element praseodymium = new Element("praseodymium", 346,460);
Element neodymium = new Element("neodymium", 402, 460);
Element promethium = new Element("promethium", 459, 460);
Element samarium = new Element("samarium", 516,460);
Element europium = new Element("europium", 573,460);
Element gadolinium = new Element("gadolinium", 630,460);
Element terbium = new Element("terbium", 687,460);
Element dysprosium = new Element("dysprosium", 743,460);
Element holmium = new Element("holmium", 801,460);
Element erbium = new Element("erbium", 857,460);
Element thulium = new Element("thulium", 914,460);
Element ytterbium = new Element("ytterbium", 971,460);
Element lutetium = new Element("lutetium", 1028,460);
Element hafnium = new Element("hafnium", 260,330);
Element tantalum = new Element("tantalum", 317,330);
Element tungsten = new Element("tungsten", 374,330);
Element rhenium = new Element("rhenium", 431,330);
Element osmium = new Element("osmium", 488,330);
Element iridium = new Element("iridium", 544, 330);
Element platinum = new Element("platinum", 601,330);
Element gold = new Element("gold", 657,330);
Element mercury = new Element("mercury", 715,330);
Element thallium = new Element("thallium", 715,330);
Element lead = new Element("lead", 829,330);
Element bismuth = new Element("bismuth", 896,330);
Element polonium = new Element("polonium", 942,330);
Element astatine = new Element("astatine", 1000,330);
Element radon = new Element("radon", 1056,330);
Element francium = new Element("francium", 89, 388);
Element radium = new Element("radium", 146,388);
Element actinium = new Element("actinium", 231,519);
Element thorium = new Element("thorium", 288,519);
Element protactinium = new Element("protactinium", 346,519);
Element uranium = new Element("uranium", 402,519);
Element neptunium = new Element("neptunium", 459,519);
Element plutonium = new Element("plutonium", 516,519);
Element americium = new Element("americium", 573,519);
Element curium = new Element("curium", 630,519);
Element berkelium = new Element("berkelium", 687,519);
Element californium = new Element("californium", 743,519);
Element einsteinium = new Element("einsteinium", 801,519);
Element fermium = new Element("fermium", 857,519);
Element mendelevium = new Element("mendelevium", 914,519);
Element nobelium = new Element("nobelium", 971,519);
Element lawrencium = new Element("lawrencium", 1028,519);
Element rutherfordium = new Element("rutherfordium", 260,388);
Element dubnium = new Element("dubnium", 317,388);
Element seaborgium = new Element("seaborgium", 374,388);
Element bohrium = new Element("bohrium", 431,388);
Element hassium = new Element("hassium", 488,388);
Element meitnerium = new Element("meitnerium", 544,388);
Element darmstadtium = new Element("darmstadtium", 601,388);
Element roentgenium = new Element("roentgenium", 657,388);
Element copernicium = new Element("copernicium", 715,388);
Element nihonium = new Element("nihonium", 771,388);
Element flerovium = new Element("flerovium", 829,388);
Element moscovium = new Element("moscovium", 886,388);
Element livermorium = new Element("livermorium", 942,388);
Element tennessine = new Element("tennessine", 1000,388);
Element oganession = new Element("oganession", 1056,388);
Element[] elements = {hydrogen,
helium,
lithium,
beryllium,
boron,
carbon,
nitrogen,
oxygen,
fluorine,
neon,
sodium,
magnesium,
aluminum,
phosphorus,
silicon,
chlorine,
argon,
potassium,
calcium,
scandium,
titanium,
vanadium,
chromium,
manganese,
iron,
cobalt,
nickel,
copper,
zinc,
gallium,
germanium,
arsenic,
selenium,
bromine,
krypton,
rubidium,
strontium,
yttrium,
zirconium,
niobium,
molybdenum,
technetium,
ruthenium,
rhodium,
palladium,
silver,
cadmium,
indium,
tin,
antimony,
tellurium,
iodine,
xenon,
cesium,
barium,
lanthanum,
cerium,
praseodymium,
neodymium,
promethium,
samarium,
europium,
gadolinium,
terbium,
dysprosium,
holmium,
erbium,
thulium,
ytterbium,
lutetium,
hafnium,
tantalum,
tungsten,
rhenium,
osmium,
iridium,
platinum,
gold,
mercury,
thallium,
lead,
bismuth,
polonium,
astatine,
radon,
francium,
radium,
actinium,
thorium,
protactinium,
uranium,
neptunium,
plutonium,
americium,
curium,
berkelium,
californium,
einsteinium,
fermium,
mendelevium,
nobelium,
lawrencium,
rutherfordium,
dubnium,
seaborgium,
bohrium,
hassium,
meitnerium,
darmstadtium,
roentgenium,
copernicium,
nihonium,
flerovium,
moscovium,
livermorium,
tennessine,
oganession };
}
public void infoBox (String info, String title){
JOptionPane.showMessageDialog(null, info, title, JOptionPane.PLAIN_MESSAGE);
}
public void update() {
//updating the background
bg.update();
}
public void draw(Graphics2D g) {
//drawing the background
bg.draw(g);
}
public void keyPressed(int k) {
if(k == KeyEvent.VK_ESCAPE){
psm.setState(ProgramStateManager.MENUSTATE);
}
}
public void keyReleased(int k) {}
public void mousePressed(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
for(int i=0; i < elements.length; i++){
if(mx > elements[i].getX() && mx < elements[i].getX() + 55){
if(my > elements[i].getY() && my < elements[i].getY() + 55){
infoBox("TEST", elements[i].getLongName());
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Hope you can help me :)
Aucun commentaire:
Enregistrer un commentaire