Problem z odwołaniem się do klasy

0

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.

0

Ja dopiero poznaje Javę ale...

Wywołujesz konstruktor klasy w niej samej - to zamierzone?

public class Hire_firm {
  ...
    public static void main(String[] args)throws IOException {
        ...
        Hire_firm load = new Hire_firm();
        load.load_user();
        load.load_vehicle();
        ....
    }
}

Pokaż może kod metody load_user.

0

Tak zamierzone tylko nie wiem czy jest to dozwolone bo też dopiero zaczynam z java.

Tutaj ta metoda o której wspomniałeś:

public void load_user()throws IOException {
		
			usertab.clear();
			
			String login, password, name, surname, authorization ;
			int id_user, ID, balance;
			
			BufferedReader text;
			try {
				  text = new BufferedReader(new FileReader("user_data.txt"));
				
				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]);
							 		
							 usertab.add(new User(id_user, login, password, name, surname, ID, authorization, balance));
							 
						 }
					
						 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);
			}
		}

Mam pewne podejrzenia dlaczego może się tak dziać zamieszczam jeszcze metoda logowania która powinna przypisywać dane obiektowi User

public boolean signin( String newlogin, String newpassword) throws IOException {
			
			int counter = 0;
			int counter1 = 0;

			for(int i=0; i<usertab.size(); i++) {
				
				if(usertab.get(i).login_in(newlogin)) {
					counter ++;
					
					
					if(usertab.get(i).passwd(newpassword)){
						
						counter1 ++;
						
						switch(usertab.get(i).check_authorization()) {
						case "U":
							user = usertab.get(i);
							break;
						case "A":
							user = usertab.get(i);
							break;
						default:
							System.out.println("Error!");
			                  return false;
						}
					}
					
				}
			}
			
			if(counter == 0 || counter1 == 0) { 
				return false;
			}
			return true;
		}

Być może źle jest przypisywane user = usertab.get(i); ale nie jestem pewien.

Metoda wpisywania do vektora:

public void load_user()throws IOException {
		
			usertab.clear();
			
			String login, password, name, surname, authorization ;
			int id_user, ID, balance;
			
			BufferedReader text;
			try {
				  text = new BufferedReader(new FileReader("user_data.txt"));
				
				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]);
							 		
							 usertab.add(new User(id_user, login, password, name, surname, ID, authorization, balance));
							 
						 }
					
						 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);
			}
		
		
		}

1 użytkowników online, w tym zalogowanych: 0, gości: 1