Und nochmal muss ich euch nerven, ihr Lieben!
Leider bin ich doch noch nicht so fit, wie ich dachte in Java.
Folgendes Problem:
Über den ActionListener möchte ich sobald der Button "btn_100pro"
gedrückt wurde, dass dieser veranlasst, dass der JSlider den Wert "100"
annimmt.
Leider geschieht das nicht, es wird ein NullPointerException in Zeile 80
geworfen ( sldDuty.setValue(100) ).
Warum? Verstehe ich ehrlichgesagt nicht!
Könntet ihr mir bitte weiterhelfen?
Vielen liebe Dank!
Hier der Java-Code, der zur NullPointerException führt. Unwichtige
Stellen im Code sind durch "..." gekennzeichnet:
1 | <<Hier sind die ganzen Imports>>
|
2 |
|
3 | public class Test_Class extends JFrame implements ActionListener
|
4 | {
|
5 | private JButton btn_100pro;
|
6 | private JSlider sldDuty;
|
7 |
|
8 | public Test_Class (String titel)
|
9 | {
|
10 | super(titel);
|
11 |
|
12 | JSlider sldDuty = new JSlider();
|
13 |
|
14 |
|
15 | btn_100pro = new JButton("100 %");
|
16 | btn_100pro.setBounds(187,373,75,30);
|
17 | btn_100pro.addActionListener(this);
|
18 | add(btn_100pro);
|
19 |
|
20 | sldDuty.setMinimum(0);
|
21 | sldDuty.setMaximum(101);
|
22 | sldDuty.setValue(0);
|
23 |
|
24 | sldDuty.setMinorTickSpacing(10);
|
25 | sldDuty.setMajorTickSpacing(20);
|
26 | ...
|
27 | add(sldDuty);
|
28 | }
|
29 |
|
30 |
|
31 | public void actionPerformed(ActionEvent e)
|
32 | {
|
33 | if(e.getSource() == btn_100pro)
|
34 | {
|
35 | sldDuty.setValue(100); // sobald auf den "100%-Button geklickt wird, soll der JSlider auf "100" gehen...tut er aber nicht...Warum?
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 | public static void main(String[] args)
|
41 | {
|
42 | Test_Class test = new Test_Class("Anwendung");
|
43 | test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
44 | test.setSize(855,582);
|
45 | test.setResizable(false);
|
46 | test.setLocationRelativeTo(null);
|
47 | test.setLayout(null);
|
48 | test.setVisible(true);
|
49 | }
|
50 | }
|
Anscheinend bin ich zu doof fürs Programmieren.
Danke euch!
Grüße