Witam. Od wczoraj walczę z problem dotyczącym odwołania się do innej klasy za pomocą obiektu.
Tutaj wklejam klasę zarządzającą:
package hireClass;
import person.Person;
import person.Guest;
import person.User;
import person.Administrator;
import vehicle.Bike;
import vehicle.Car;
import vehicle.Motorcycle;
import vehicle.Truck;
import vehicle.Vehicle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
import main_menu.MainMenuFrame;
import main_menu.SignInFrame;
public class Hire_firm {
List<User>usertab = new ArrayList<User>();
List<Vehicle>vehicleTab =new ArrayList<Vehicle>();
User user;
Administrator admin;
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
MainMenuFrame.main(null);
Hire_firm load = new Hire_firm();
load.load_user();
load.load_vehicle();
}
public boolean cash(int cash)throws IOException {
return user.pay(cash);
}
}
Tutaj klasa do której się odwołuję:
package person;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.io.File;
import javax.swing.JOptionPane;
import hireClass.Hire_firm;
public class User extends Guest {
public User(int id_user, String login, String password, String name, String surname, int ID, String authorization, int balance) {
super(id_user, login, password, name, surname, ID, authorization, balance);
this.id_user = id_user;
this.login = login;
this.password = password;
this.name = name;
this.surname = surname;
this.ID = ID;
this.authorization = authorization;
this.balance = balance;
}
public boolean pay(int cash)throws IOException{
String Login, Password, Name, Surname, Authorization ;
int ID_user, id, Balance;
int newcash = balance - cash;
try {
BufferedReader text = new BufferedReader(new FileReader("user_data.txt"));
try {
BufferedWriter text_tmp = new BufferedWriter(new FileWriter("user_data1.txt", true));
String line;
String[] tokens;
while((line = text.readLine()) != null) {
tokens = line.split(" ");
ID_user = Integer.parseInt(tokens[0]);
Login = tokens[1];
Password = tokens[2];
Name = tokens[3];
Surname = tokens[4];
id = Integer.parseInt(tokens[5]);
Authorization = tokens[6];
Balance = Integer.parseInt(tokens[7]);
if(login.equals(Login)) {
text_tmp.write(ID_user + " " + Login + " " + Password + " " + Name
+ " " + Surname + " " + id + " " + Authorization + " " + newcash+ "\n");
continue;
}
else {
text_tmp.write(ID_user + " " + Login + " " + Password + " " + Name
+ " " + Surname + " " + id + " " + Authorization + " " + Balance+ "\n");
}
}
try{
}catch (IllegalArgumentException e) {
System.out.println("Error!");
}finally{
try{
text_tmp.close();
text.close();
}catch (IOException e) {
System.out.println("Error wrong close!");
System.exit(3);
}
}
try{
File file = new File("user_data.txt");
File file_tmp = new File("user_data1.txt");
if(!file.delete()){
JOptionPane.showMessageDialog(null, "Wrong delete user");
return false;
}
try {
if(!file_tmp.renameTo(file)){
JOptionPane.showMessageDialog(null, "Wrong rename");
return false;
}
try {
File file_tm = new File("user_data1.txt");
if (!file_tm.createNewFile()){
JOptionPane.showMessageDialog(null, "wrong create usertm");
return false;
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
try{
}catch (IllegalArgumentException e) {
System.out.println("Error!");
}finally{
try{
text_tmp.close();
text.close();
}catch (IOException e) {
System.out.println("Error wrong close!");
System.exit(3);
}
}
}
catch(FileNotFoundException e) {
System.out.println("Error file not found" +e);
}
try{
}catch (IllegalArgumentException e) {
System.out.println("Error!");
}finally{
try{
text.close();
}catch (IOException e) {
System.out.println("Error wrong close!");
System.exit(3);
}
}
}
catch(FileNotFoundException e) {
System.out.println("Error file not found" +e);
}
return true;
}
}
Problem pojawia się w tym momencie:
public boolean cash(int cash)throws IOException {
return user.pay(cash);
}
A mianowicie kompilator wyrzuca mi taki błąd:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at hireClass.Hire_firm.cash(Hire_firm.java:751)
at user_windows.PayFrame$3.actionPerformed(PayFrame.java:92)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Tutaj jeszcze załączam pełen kod frame'u który ma obsłługiwać to zdarzenie.
Gdy użyje zamiast :
return user.pay(cash);
komendy:
return user.get(0).pay(cash)
wszystko działa poprawnie. Z góry dziękuje za wyjaśnienie dlaczego tak się dzieje i za ewentualne porady co do powyższego kodu.