سؤال

I am having trouble displaying values stored in an array on a Java swing jFrame, I am trying to display the values as jLabels within the jFrame.

I want the details of the first car to display when the form loads, other cars in the array to display when you click a 'next' (record) button.

The Car class:

package abc;
import java.io.*;
import java.util.*;

public class Car {

private String carReg, carModel, carMake;
private double carValue;

public void setCarReg(String carReg){
    this.carReg = carReg;
}
public String getCarReg(){
    return this.carReg;
}

public void setCarModel(String carModel){
    this.carModel = carModel;
}
public String getCarModel(){
    return this.carModel;
}

public void setCarMake(String carMake){
    this.carMake = carMake;
}
public String getCarMake(){
    return this.carMake;
}

public void setCarValue(double carValue){
    this.carValue = carValue;
}
public double getCarValue(){
    return this.carValue;
}

public static ArrayList<Car> CarArray = new ArrayList<Car>();
Iterator I = CarArray.iterator();

public static void exportCar(){
    String fileLoc = "C:\\abc\\exportCar.txt";
    int i = 0;

    try
    {
        FileOutputStream uh = new FileOutputStream(fileLoc);
        ObjectOutputStream st = new ObjectOutputStream(uh);

        for (i=0; i<CarArray.size(); i++)
        {
            st.writeObject(CarArray.get(i));
        }
    }
    catch(IOException e)
    {
          System.out.println(e);      
    }
}

public static void importCar(){
    String fileLoc = "C:\\abc\\exportCar.txt";
    int i = 0;

    try
    {
        FileInputStream uh = new FileInputStream(fileLoc);
        ObjectInputStream st = new ObjectInputStream(uh);

        try
        {
            while(uh!=null)
            {
                CarArray.add((Car)st.readObject());
            }
        }
        catch(Exception e){}
    }
    catch(IOException e)
    {
          System.out.println(e);      
    }
}
}

The Login jForm:

package abc;
import java.util.*;

public class Login extends javax.swing.JFrame {
// Bidder bidder1 = new Bidder("cm", "w", 3, "FN", "LN", 1, "Street", "City", "BA99AE", 
//"1234567890", "EMail", "Card Make", "123456", "1122", 123);
public static int userLevelFound = 0;
/** Creates new form Login */
public Login() {

initComponents();
    Judge.JudgeArray.add(judge1);
    Representative.RepArray.add(rep1);
    //Bidder.BidderArray.add(bidder1);
    Bidder.importBidder();
    Car.importCar();
    LoginError.setVisible(false);
}

 private void LoginActionPerformed(java.awt.event.ActionEvent evt) {                                      
// Get the username entered
    String inputUN = UserName.getText();

// Get the password entered (plain text)
    String inputPassword = Password.getText();

    boolean loginFound = false;


    int a = 0;
    int b = Judge.JudgeArray.size();
    do
    {
        Judge tempJudge = new Judge();
        tempJudge = Judge.JudgeArray.get(a);
        if (inputUN.equals(tempJudge.getUserName()) && 
                inputPassword.equals(tempJudge.getPassword())){
            loginFound = true;
            userLevelFound = 1;
        }
        a++;
    } while ((a < b) && (loginFound == false));

    a = 0;
    b = Representative.RepArray.size();
    do
    {
        Representative tempRep = new Representative();
        tempRep = Representative.RepArray.get(a);
        if (inputUN.equals(tempRep.getUserName()) && 
                inputPassword.equals(tempRep.getPassword())){
            loginFound = true;
            userLevelFound = 2;
        }
        a++;
    } while ((a < b) && (loginFound == false));

    a = 0;
    b = Bidder.BidderArray.size();
    do
    {
        Bidder tempBidder = new Bidder();
        tempBidder = Bidder.BidderArray.get(a);
        if (inputUN.equals(tempBidder.getUserName()) && 
                inputPassword.equals(tempBidder.getPassword())){
            loginFound = true;
            userLevelFound = 3;
        }
        a++;
    } while ((a < b) && (loginFound == false));
    if (loginFound == true){
        Home home = new Home();
        home.setVisible(true);
        this.setVisible(false);
    } else{
        LoginError.setVisible(true);
    }
}                             

The Home jForm:

package abc;
import java.util.*;

public class Home extends javax.swing.JFrame {
ArrayList<Car> carList = new ArrayList<Car>();
Iterator I;
/** Creates new form Home */
public Home() {
    initComponents();
    if (Login.userLevelFound == 3)
    {
        addCar.setVisible(false);
        editCar.setVisible(false);
    }

}

private void editCarActionPerformed(java.awt.event.ActionEvent evt) 
{                                        
    // TODO add your handling code here:
    Edit updateCar = new Edit(carList);
    updateCar.setVisible(true);
    this.dispose();
}  

The Edit Cars jForm:

package abc;

import java.util.*;

public class Edit extends javax.swing.JFrame {
ArrayList<Car> carList;
Iterator I;

Car editCar = new Car();


/** Creates new form Edit */
public Edit(ArrayList eC) {
    initComponents();
    carList = eC;
    I = carList.iterator();
    if (Login.userLevelFound == 2)
    {
        carValue.setVisible(false);
        jLabel4.setVisible(false);
    }

}                                     

private void backActionPerformed(java.awt.event.ActionEvent evt)  
{                                     
    // TODO add your handling code here:
    Home home = new Home();
    home.setVisible(true);
    this.dispose();
}                                    

private void SaveActionPerformed(java.awt.event.ActionEvent evt)
{                                     
    // TODO add your handling code here:

}                                    

private void nextActionPerformed(java.awt.event.ActionEvent evt)
{                                     
    // TODO add your handling code here:
    if(I.hasNext()){
    editCar = (Car)I.next();
    carMake.setText(editCar.getCarMake());
    carModel.setText(editCar.getCarModel());
    carReg.setText(editCar.getCarReg());
    carValue.setText("£" + editCar.getCarValue());
}    

I'm pretty sure I haven't initialised the array properly, struggling to grasp how to do this in all honesty.

هل كانت مفيدة؟

المحلول

There it is, ArrayList<Car> carList is never initialized.

Your code:

public class Home extends javax.swing.JFrame {
    ArrayList<Car> carList;

Correct code:

public class Home extends javax.swing.JFrame {
    ArrayList<Car> carList = new ArrayList<Car>();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top