Forum: PC-Programmierung Java Code ausbessern


von Javanier (Gast)


Lesenswert?

Hallo,

ich steige gerade in die Java Programmierung ein und baue seriell eine 
Verbindung auf. Eigentlich bekomme ich schon Daten und werden in eine 
Datei geschrieben, jedoch werden mir Warnungen angezeigt wo ich nicht 
Durchblicke woran das liegen könnte.

Somit wollte ich fragen wie ich die Warnungen beheben kann und wie ich 
den Code noch verbessern könnte. Es sind nur die grundlegenden 
Funktionen vorhanden und die Funktion wird nicht ausgeführt wann Daten 
kommen sondern führt ständig while(1) aus.

Für Tipps bin ich euch dankbar.

MfG
1
import java.io.*;
2
import gnu.io.CommPort;
3
import gnu.io.CommPortIdentifier;
4
import gnu.io.SerialPort;
5
import gnu.io.NoSuchPortException;
6
import gnu.io.PortInUseException;
7
import gnu.io.UnsupportedCommOperationException;
8
9
/**
10
 *
11
 * @author
12
 */
13
public class UsbStick {
14
    
15
   private static SerialPort serialPort = null;
16
    
17
    
18
    public void dateiBeschreiben(String zeile){
19
     ...
20
    }
21
    
22
    public void verbinden(){
23
        CommPortIdentifier portIdentifier = null;
24
        CommPort commPort = null;
25
        
26
        try{
27
            portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
28
        }catch(NoSuchPortException e){
29
            System.out.println(e);
30
        }
31
        
32
        //Open the port and give the port name and a timeout 
33
        try{
34
            commPort = portIdentifier.open("/dev/ttyUSB0", 2000);  //Warning: Dereferencing possible null pointer
35
        }catch(PortInUseException e){
36
            System.out.println(e);
37
        }
38
        
39
40
        this.serialPort = (SerialPort) commPort; //Warning: Accessing static field serialPort
41
42
        //Set the baud rate and other parameters of the serial port
43
        try{
44
            this.serialPort.setSerialPortParams(115200, 8, 1, 0);  //Warning: Accessing static field serialPort
45
46
        }catch(UnsupportedCommOperationException e){
47
            System.out.println(e);
48
        }
49
}
50
51
    /**
52
     * @param args the command line arguments
53
     */
54
    public static void main(String[] args){
55
        // TODO code application logic here
56
        UsbStick beschreiben = new UsbStick();
57
  
58
        beschreiben.verbinden();
59
    
60
        
61
        InputStream in = null; //Warning: The assigned value is never used
62
        String zeile = null;   //Warning: The assigned value is never used
63
        BufferedReader reader = null;  //Warning: The assigned value is never used
64
        while(true){
65
  
66
                in = this.serialPort.getInputStream(); //Error: non static variable this cannot be referenced from a static context
67
                if (in != null){
68
                    try{
69
                        reader = new BufferedReader(new InputStreamReader(in));
70
                        zeile = reader.readLine();
71
                        System.out.println(zeile);
72
                        beschreiben.dateiBeschreiben(zeile);
73
                    }catch(IOException e){
74
                        System.out.println("IOException");
75
                    }          
76
                }
77
        }        
78
    }  
79
}

von Udo S. (urschmitt)


Lesenswert?

Habe mir nur den ersten angesehen:

Javanier schrieb:
> //Warning: Dereferencing possible null pointer

Du holst dir "portIdentifier", aber was machst du wenn das nicht 
funktioniert?
Nichts du druckst die Exception (besser wäre hier e.printStackTrace();) 
und willst dann fröhlich weitermachen obwohl durch den fehler 
"portIdentifier" null ist.
Richtig wäre hier die Exception (oder eine eigene weiter nach "oben" zu 
werfen. Oder aber zumindest die Funktion mit einem Fehlercode 
abzubrechen, den du dann auch auswerten müsstest.

Javanier schrieb:
> try{
>             portIdentifier =
> CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
>         }catch(NoSuchPortException e){
>             System.out.println(e);
>         }

von Wühlhase (Gast)


Lesenswert?

Ich würde dir raten, mal ein Buch über Entwurfsmuster zu lesen. 
Vielleicht "Entwurfsmuster von Kopf bis Fuß", das arbeitet mit Java und 
ich finde es sehr gut.

Danach wirst du einige Anhaltspunkte haben, was du an deinem Code ändern 
solltest.

von oerks (Gast)


Lesenswert?

Warnungen aller Art einfach nach /dev/null umleiten.
Problem geloest!

von Javanier (Gast)


Lesenswert?

@Udo S.
Danke für den guten Tipp.

Hat sonst noch jemand Tipps wie man die Programmierung von der 
Verbindung verbessern kann!
1
import java.io.*;
2
3
import gnu.io.CommPort;
4
5
import gnu.io.CommPortIdentifier;
6
7
import gnu.io.SerialPort;
8
9
import gnu.io.NoSuchPortException;
10
11
import gnu.io.PortInUseException;
12
13
import gnu.io.UnsupportedCommOperationException;
14
15
/**
16
17
 *
18
19
 * @author
20
21
 */
22
23
public class UsbStick {
24
25
    
26
27
   private static SerialPort serialPort = null;
28
29
    
30
31
    
32
33
    public void dateiBeschreiben(String zeile){
34
35
     ...
36
37
    }
38
39
    
40
41
    public void verbinden(){
42
43
        CommPortIdentifier portIdentifier = null;
44
45
        CommPort commPort = null;
46
47
        
48
49
        try{
50
51
            portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
52
53
        }catch(NoSuchPortException e){
54
55
            e.printStackTrace();
56
57
        }
58
59
        
60
61
        //Open the port and give the port name and a timeout 
62
63
        try{
64
65
            commPort = portIdentifier.open("/dev/ttyUSB0", 2000);  //Warning: Dereferencing possible null pointer
66
67
        }catch(PortInUseException e){
68
69
            e.printStackTrace();
70
71
        }
72
73
        
74
75
        this.serialPort = (SerialPort) commPort; //Warning: Accessing static field serialPort
76
77
        //Set the baud rate and other parameters of the serial port
78
79
        try{
80
81
            this.serialPort.setSerialPortParams(115200, 8, 1, 0);  //Warning: Accessing static field serialPort
82
83
        }catch(UnsupportedCommOperationException e){
84
85
            e.printStackTrace();
86
87
        }
88
89
}
90
91
    /**
92
93
     * @param args the command line arguments
94
95
     */
96
97
    public static void main(String[] args){
98
99
        // TODO code application logic here
100
101
        UsbStick beschreiben = new UsbStick();
102
103
  
104
105
        beschreiben.verbinden();
106
107
    
108
109
        
110
111
        InputStream in = null; //Warning: The assigned value is never used
112
113
        String zeile = null;   //Warning: The assigned value is never used
114
115
        BufferedReader reader = null;  //Warning: The assigned value is never used
116
117
        while(true){
118
119
  
120
121
                in = this.serialPort.getInputStream(); //Error: non static variable this cannot be referenced from a static context
122
123
                if (in != null){
124
125
                    try{
126
127
                        reader = new BufferedReader(new InputStreamReader(in));
128
129
                        zeile = reader.readLine();
130
131
                        System.out.println(zeile);
132
133
                        beschreiben.dateiBeschreiben(zeile);
134
135
                    }catch(IOException e){
136
137
                        e.printStackTrace();
138
139
                    }          
140
141
                }
142
143
        }        
144
145
    }  
146
147
}

von quotendepp (Gast)


Lesenswert?

sind die warning und error kommentare fehlermeldungen des compilers?

wenn ja: lösen!

sonst geht da nie was weiter

von Jonas B. (jibi)


Lesenswert?

Dann benutz doch auch deine static variable, anstatt eine lokale 
Variable zu benutzen. Die anderen Fehler kannst du ignorieren, die 
kommen weil du die Variablen vor einer Schleife initialisiert und bevor 
du den Wert jemals ausliest einen neuen zuweist.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.