Hallo Leute,
hab folgenden Code geschrieben:
Der Code lässt sich zwar Probelmlos starten, wenn mann aber nach dem Start den "Startbutton" Drückt kommt folgende Fehlermeldung:
Die variable
zum setzen der RGB Werten ist vorhanden und müsste eigl. schon vor dem aufruf zum setzen der Farbe, initialisiert werden.
Ich kann mir irgendwie nicht erklären warum die Fehlermeldung aufgeworfen wird.
Ich hoffe ihr könnt mir Helfen.
Lg
tron36
hab folgenden Code geschrieben:
Java:
package Aufgaben;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
public class Valar extends JFrame {
private JPanel contentPane;
private JPanel checkpanel;
private JPanel panelDraw;
private JButton btnStart;
private JRadioButton rdbtnJa;
private JRadioButton rdbtnNein;
private ButtonGroup btnGrp;
private JLabel pointstr;
private drawPoint point;
private String status;
private String answer;
private int diff;
private int sec;
private int count;
/**
* Launch the application.
*/
public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// Valar frame = new Valar();
// frame.setVisible(true);
// btnStart.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// createLightPoint();
// }
// });
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
new Valar().run();
}
/**
* Create the frame.
*/
public Valar() {
setTitle("Aufgabe 1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
// this.setVisible(true);
contentPane = new JPanel(new BorderLayout());
setContentPane(contentPane);
btnStart = new JButton("Start");
btnStart.setBounds(0, 0, 434, 23);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Event listner StartButton");
createLightPoint();
}
});
contentPane.add(btnStart);
checkpanel = new JPanel();
checkpanel.setBounds(0, 229, 434, 33);
contentPane.add(checkpanel);
rdbtnJa = new JRadioButton("Ja");
rdbtnJa.setSelected(false);
checkpanel.add(rdbtnJa);
rdbtnNein = new JRadioButton("Nein");
rdbtnNein.setSelected(false);
checkpanel.add(rdbtnNein);
btnGrp = new ButtonGroup();
btnGrp.add(rdbtnJa);
btnGrp.add(rdbtnNein);
panelDraw = new JPanel(new BorderLayout());
panelDraw.setForeground(Color.BLACK);
panelDraw.setBackground(Color.BLACK);
panelDraw.setBounds(0, 22, 434, 206);
contentPane.add(panelDraw);
// point = new point();
// point.setVisible(false);
// point.setAlignmentX(alignmentX);
// point.setHorizontalAlignment(JLabel.CENTER);
// point.setVerticalAlignment(JLabel.CENTER);
// panelDraw.add(point, BorderLayout.CENTER);
pointstr = new JLabel("X");
pointstr.setVisible(true);
pointstr.setForeground(Color.WHITE);
pointstr.setHorizontalAlignment(JLabel.CENTER);
pointstr.setVerticalAlignment(JLabel.CENTER);
panelDraw.add(pointstr, BorderLayout.CENTER);
status = new String();
}
public void run() {
// Valar frame = new Valar();
setVisible(true);
point = new drawPoint();
System.out.println(point);
point.setBackground(new Color(sec, sec, sec));
point.setPreferredSize(new Dimension(434, 206));
panelDraw.add(point, BorderLayout.CENTER);
point.setVisible(false);
//Variablen
diff = 0; //differenz zum ändern der Farbe
sec = 0; //zum setzen der Farbe des Punktes
count = 0; //zum zählen der Durchläufe
}
//-----------------------------------------------------------------------------
private void createLightPoint() {
//-----------------------------------------------------------------------------
System.out.println("createLightPoint()");
btnGrp.clearSelection();
sec = sec + diff;
System.out.println("Farbe " + sec);
System.out.println("Point " + point);
checkpanel.setVisible(false);
pointstr.setVisible(false);
point.setForeground(new Color(sec, sec, sec));
point.setVisible(true);
try {
Thread.sleep(2000);
}catch(InterruptedException e) {
e.printStackTrace();
}
point.setVisible(false);
pointstr.setVisible(true);
checkpanel.setVisible(true);
choice();
}
//-----------------------------------------------------------------------------
public void choice() {
//-----------------------------------------------------------------------------
do {
rdbtnJa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Radiobutton Ja");
if(status.equals("ueberschwellig")) {
diff = -20;
} else {
sec = 255;
diff = 0;
status = "ueberschwellig";
}
answer = "JA";
}
});
//Nochmalls anschauen:
rdbtnNein.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Radiobutton Nein");
if(status.equals("ueberschwellig")) {
sec = 0;
diff = 0;
status = "unterschwellig";
} else {
diff = 20;
}
answer = "NEIN";
}
});
count++;
}while(btnGrp.getSelection() == null /*&& count <= 6*/);
System.out.println("Light Point " + status + " " + sec + " " + answer + " " + diff);
//in .txt Datei schreiben!
//createLightPoint();
}
}
class drawPoint extends JPanel{
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D pointer = (Graphics2D) g;
pointer.setColor(Color.WHITE);
pointer.drawOval(0, 0, 1, 1);
// setSize(1, 1);
}
}
Der Code lässt sich zwar Probelmlos starten, wenn mann aber nach dem Start den "Startbutton" Drückt kommt folgende Fehlermeldung:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Aufgaben.Valar.createLightPoint(Valar.java:142)
at Aufgaben.Valar$1.actionPerformed(Valar.java:75)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6397)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Die variable
Code:
sec
Ich kann mir irgendwie nicht erklären warum die Fehlermeldung aufgeworfen wird.
Ich hoffe ihr könnt mir Helfen.
Lg
tron36
Zuletzt bearbeitet: