简体   繁体   中英

Object attribute returns as null

This is my first post here so please go easy on me, if I do some formal mistakes. My problem is, that an attribute of the class Customer returns null, despite the fact, that it will be created from the constructor of the Customer class itself. I dont understand how this is possible, it should be there. Maybe you can help me to understand.

This is the Customer class, that after creation should have an attribute of the type Attachement.

package model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class Customer {

private String name;
private String projectNr;
private String[] aliases;
private String[] blocks;
private Attachment attachement;

public Customer(String name, String projectNr, String[] aliases, String[] blocks, ArrayList<TransactionFile> allFiles) {
    super();
    this.name = name;
    this.projectNr = projectNr;
    this.aliases = aliases;
    this.blocks = blocks;
    this.attachement = new Attachment(this, allFiles);

}

@Override
public String toString() {
    return "Customer [name=" + name + ", projectNr=" + projectNr + ", aliases=" + Arrays.toString(aliases)
            + ", blocks=" + Arrays.toString(blocks) + "]";
}

public Attachment getAttachement() {
    return attachement;
}

public void setAttachement(Attachment attachement) {
    this.attachement = attachement;
}

public String[] getAliases() {
    return aliases;
}

public void setAliases(String[] aliases) {
    this.aliases = aliases;
}

private Attachment attachment;

public String getProjectNr() {
    return projectNr;
}

public void setProjectNr(String projectNr) {
    this.projectNr = projectNr;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String[] getBlocks() {
    return blocks;
}

public void setBlocks(String[] blocks) {
    this.blocks = blocks;
}

public Attachment getAttachment() {
    return attachment;
}

public void setAttachment(Attachment attachment) {
    this.attachment = attachment;
}

}

This is the Attachement class. In some try outs, I veryfied with a sysout at the end of the constructor, that the object is build.

    package model;

import java.util.ArrayList;

import controller.AttachmentBuilder;

public class Attachment{

private Customer customer;
private ArrayList<FixedPosition> fixedPositions;
private ArrayList<TransactionFile> transactionFiles;

public Attachment(Customer customer, ArrayList<TransactionFile> allFiles) {
    super();
    this.customer = customer;
    this.fixedPositions = null;
    this.transactionFiles = new AttachmentBuilder(allFiles).createTransactionList(this.customer);
    
}

@Override
public String toString() {
    return "Attachment [customer=" + customer + ", fixedPositions=" + fixedPositions + ", transactionFiles="
            + transactionFiles + "]";
}

public Customer getCustomer() {
    return customer;
}

public void setCustomer(Customer customer) {
    this.customer = customer;
}

public ArrayList<FixedPosition> getFixedPositions() {
    return fixedPositions;
}

public void setFixedPositions(ArrayList<FixedPosition> fixedPositions) {
    this.fixedPositions = fixedPositions;
}

public ArrayList<TransactionFile> getTransactionFiles() {
    return transactionFiles;
}

public void setTransactionFiles(ArrayList<TransactionFile> transactionFiles) {
    this.transactionFiles = transactionFiles;
}


}

When I run this part after creating my Customer Array, I will get null for the customer.getAttachement(). For the record, it has a toString().

package controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.sql.Timestamp;
import java.util.ArrayList;


import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import model.Customer;
import model.TransactionFile;

public class Application {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    
    
    SshConnector sshConnector = new SshConnector();
    sshConnector.openSession();
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    Date date = new Date(ts.getTime());
    System.out.println(date);
    
    AttachmentBuilder ab = new AttachmentBuilder(sshConnector.getTransactions("2109"));
    ab.serializeFiles(sshConnector.getFolder());        
    sshConnector.closeSession();
    ts = new Timestamp(System.currentTimeMillis());
    date = new Date(ts.getTime());
    System.out.println(date);
    
    // build the customers
    String fileContent="";
    ArrayList<Customer> customerList = new ArrayList<>(); 
    
    File file = new File("src/resources/customer.json");
    try {
        fileContent = FileUtils.readFileToString(file, "utf-8");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    System.out.println("Baue JSON array");
    JSONArray customerArray = new JSONArray(fileContent);
    
    for (int i = 0; i < customerArray.length(); i++) {
        JSONObject tmpJsonObject = customerArray.getJSONObject(i);
        JSONArray tmpAliases = tmpJsonObject.getJSONArray("aliases");
        String aliases [] = new String [tmpAliases.length()];
        for(int j=0; j<tmpAliases.length(); j++) {
            aliases[j] = tmpAliases.getString(j);           
        }
        JSONArray tmpBlocks = tmpJsonObject.getJSONArray("blocks");
        String blocks [] = new String [tmpBlocks.length()];
        for(int j=0; j<tmpBlocks.length(); j++) {
            blocks[j] = tmpBlocks.getString(j);         
        }
        
        customerList.add(new Customer(tmpJsonObject.getString("name"), tmpJsonObject.getString("projectNr"),aliases, blocks, ab.getAllFiles()));
    }
    System.out.println("gebe customer aus");
    for (Customer customer : customerList) {
        System.out.println(customer);
        System.out.println(customer.getAttachment());
    }
    
    System.exit(0);

This is the AttachementBuilder

    package controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import model.Customer;

import model.TransactionFile;

public class AttachmentBuilder {

private  ArrayList<TransactionFile> allFiles;

public AttachmentBuilder(ArrayList<TransactionFile> allFiles) {
    super();
    this.allFiles = allFiles;
}

public ArrayList<TransactionFile> getAllFiles() {
    return allFiles;
}

public void setAllFiles(ArrayList<TransactionFile> allFiles) {
    this.allFiles = allFiles;
}

/*
 * writes the objects from the ArrayList that holds the transactions into a txt
 * file only for testing purpose
 * 
 * @Param ArrayList containing the files to serialize
 * 
 * @Param String substring to complete the file name usualy the folder that had
 * the actual month
 */
public void serializeFiles(String folder) {
    try {
        File fakturaRaw = new File("faktura" + folder + ".txt");
        System.out.println("Schreibe File");
        FileWriter writer = new FileWriter(fakturaRaw);
        for (TransactionFile transactionFile : allFiles) {
            writer.write(transactionFile.toString() + System.lineSeparator());
        }
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public ArrayList<TransactionFile> createTransactionList(Customer customer) {
    ArrayList<TransactionFile> list = new ArrayList<TransactionFile>();

    if (customer.getBlocks() != null) {

    } else {
        for (int i = 0; i < this.allFiles.size(); i++) {
            
            for (int j = 0; j < customer.getAliases().length; j++) {
                
                if (this.allFiles.get(i).getFileName().contains(customer.getAliases()[j])) {
                    list.add(allFiles.get(i));
                }
            }
        }
    }

    return list;
}

}

    

In order to use super in java you need to inherit a class, I guess what You're trying to do is to extend the costumer class, so you need to change the class from

public class Attachment

to

public class Attachment extends Costumer

Then you need to use the super() to call the copy constructor in Attachment class like this:

super(customer);

Adding the customer object will tell the compiler to pick the copy constructor. Which will be in your case like:

public Customer(Customer other) {
    //deep copy
 }

Finally you don't need to declare an Attachment object in Customer class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM