Cześć!
Ogólnym zadaniem programu jest otwieranie i edycja plików tekstowych z menu czyli dopisywanie i usuwanie linii tekstu oraz dodawanie nowych plików do menu. Usuwanie i sprawdzanie czy plik jest w menu nie działa poprawnie. środowisko to NeatBeans 8.2
chodzi mi o linijki 65 i 121 gdzie porównuje to co wpisał użytkownik z tym co jest w pliku. Problem jest ten sam w 2 przypadkach obydwa podejścia nie działają .equals wyrzuca błąd java.lang.nullpointerexception a == kompletnie pomija i metoda wykonuje się tak jakby bez if i to jak zadeklaruje r tak je zwróci.
Proszę o pomoc bo na prawdę skończyły mi się pomysły.
tutaj wyodrębnione błędy które zauważyłem
if(s.equals(u)){
pw.println("");
}
if(s==w){
r++;
}
tutaj kod całego programu
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.File;
public class Zaliczenie extends java.applet.Applet {
String plik="";
void odczyt(String plik){
String s;
try{
FileReader fr = new FileReader(plik+".txt");
BufferedReader br = new BufferedReader(fr);
do{
s = br.readLine();
if(s != null){
textArea1.append(s+"\n");
}
}while(s != null);
fr.close();
br.close();
}catch(IOException e){
System.out.println("Błąd we/wy...");
}
}
void dopisz(String s, String p){
try{
FileWriter fw = new FileWriter(p+".txt",true);
PrintWriter pw = new PrintWriter(fw);
pw.println(s);
//textArea1.append(s);
pw.close();
fw.close();
}catch(IOException e){
System.out.println("błąd wejścia wyjścia");
}
}
void usun(String u){
//String s="";
try{
String s="";
FileReader fr = new FileReader(plik+".txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("buf.txt",true);
PrintWriter pw = new PrintWriter(fw);
do{
s = br.readLine();
if(s.equals(u)){
pw.println("");
}
else{
if(s != null){
pw.println(s);
}
}
}while(s != null);
fr.close();
br.close();
pw.close();
fw.close();
File n = new File(plik+".txt");
n.delete();
FileReader fr1 = new FileReader("buf.txt");
BufferedReader br1 = new BufferedReader(fr1);
FileWriter fw1 = new FileWriter(plik+".txt",true);
PrintWriter pw1 = new PrintWriter(fw1);
do{
s = br1.readLine();
if(s != null){
pw1.println(s);
}
}while(s != null);
fr1.close();
br1.close();
fw1.close();
pw1.close();
File b = new File("buf.txt");
b.delete();
}catch(IOException e){
System.out.println("Błąd we/wy...");
}
}
int spr(String w){
try{
int r = 1 ;
String s;
FileReader fr = new FileReader("menu.txt");
BufferedReader br = new BufferedReader(fr);
do{
s = br.readLine();
if(s==w){
r++;
}
}while(s != null);
fr.close();
br.close();
return r;
}catch(IOException e){
System.out.println("Błąd we/wy...");
return 0;
}
}
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This method is called from within the init() method to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
label1 = new java.awt.Label();
textArea1 = new java.awt.TextArea();
button1 = new java.awt.Button();
button2 = new java.awt.Button();
button3 = new java.awt.Button();
label2 = new java.awt.Label();
button4 = new java.awt.Button();
textField2 = new java.awt.TextField();
button5 = new java.awt.Button();
button1.setLabel("Otwórz");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button2.setLabel("Dopisz");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
button3.setLabel("Usuń");
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button3ActionPerformed(evt);
}
});
button4.setLabel("Menu");
button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button4ActionPerformed(evt);
}
});
textField2.setText("Gry");
button5.setLabel("Utwórz nowy plik");
button5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button5ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(145, 145, 145)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(button2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(label2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(146, 146, 146)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(textArea1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
.addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(205, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(81, 81, 81)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(64, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
textArea1.setText("");
plik=textField2.getText();
if(plik.equals("")){
textArea1.setText("Podaj nazwę pliku");
}
else if (spr(plik)==0){
textArea1.setText("Pliku nie ma w bazie");
}
else{
odczyt(plik);
label1.setText("Jesteś w:");
label2.setText(plik);
textField2.setText("");
}
}//GEN-LAST:event_button1ActionPerformed
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
textArea1.setText("");
if(textField2.getText().equals("")){
textArea1.setText("podaj tekst");
}
else if(plik.equals("")){
textArea1.setText("nie jesteś w pliku");
}
else{
label1.setText("Jesteś w:");
label2.setText(plik);
dopisz(textField2.getText(), plik);
textField2.setText("");
textArea1.setText("");
odczyt(plik);
}
}//GEN-LAST:event_button2ActionPerformed
private void button3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button3ActionPerformed
textArea1.setText("");
usun(textField2.getText());
odczyt(plik);
}//GEN-LAST:event_button3ActionPerformed
private void button4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button4ActionPerformed
textArea1.setText("");
label1.setText("Jesteś w:");
label2.setText("menu czytasz "+plik);
odczyt("menu");
}//GEN-LAST:event_button4ActionPerformed
private void button5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button5ActionPerformed
textArea1.setText("");
dopisz(textField2.getText(), "menu");
odczyt("menu");
label1.setText("Jesteś w:");
label2.setText("menu czytasz "+plik);
dopisz("", textField2.getText());
textField2.setText("");
}//GEN-LAST:event_button5ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private java.awt.Button button1;
private java.awt.Button button2;
private java.awt.Button button3;
private java.awt.Button button4;
private java.awt.Button button5;
private java.awt.Label label1;
private java.awt.Label label2;
private java.awt.TextArea textArea1;
private java.awt.TextField textField2;
// End of variables declaration//GEN-END:variables
}