Hej,
Mam małą zagwostkę. Poniższy kod ma pobierać wartości z dwóch jFrame'ów (VisticUI2 i VisticUI3), ale tylko jeden jest otwarty w danym czasie. Pojawia się teraz problem, bo po odpaleniu jButton1 wywala mi błąd "NullPointerException" już na linijce "String tekstID = VisticUI2.labelID.getText();". Ale właśnie tak powinno być, że skoro VisticUI2 nie jest otwarte, to tekstID = null i w bloku "if.. else if.." wykonywana jest instrukcja "else if (tekstID2 != null && tekstID == null)" dla wariantu kiedy tekstID jest pusty, a tekstID2 nie. Jak to ugryźć, by przy tekstID = null nie przerywało programu, tylko spróbowało przeczytać zawartość VisticUI3.labelID2 i wykonało dalsze instrukcje? Poniżej kod.
Pozdrawiam serdecznie! :)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String tekst = jLabel2.getText();
if (tekst.equals("1"))
{
// Pobieram wartości ze statycznych pól dwóch klas
String tekstID = VisticUI2.labelID.getText();
String tekstID2 = VisticUI3.labelID2.getText();
// W bloku "if.. else if.." chcę wykonać operacje w zależności, czy VisticUI2 jest otwarte, czy VisticUI3
if (tekstID != null && tekstID2 == null)
{
File perimetry1;
perimetry1 = new File("Database/" + tekstID, "Perimetry 1366 x 768.txt");
try
{
perimetry1.createNewFile();
}
catch (IOException ex)
{
Logger.getLogger(NewSession.class.getName()).log(Level.SEVERE, null, ex);
}
new PerimetryWindow1().setVisible(true);
}
else if (tekstID2 != null && tekstID == null)
{
File perimetry2;
perimetry2 = new File("Database/" + tekstID2, "Perimetry 1366 x 768.txt");
try
{
perimetry2.createNewFile();
}
catch (IOException ex)
{
Logger.getLogger(NewSession.class.getName()).log(Level.SEVERE, null, ex);
}
new PerimetryWindow1().setVisible(true);
}
}
}