3

I am developing an API with spring-boot.Basically I want to get all the values from PortedNumber class from Service class and store them to a text file.

public class Service {
    PortedNumber portedNumber = new PortedNumber();
    public AcknowledgeMessage checkRequestValidity(Request request) throws IOException {
        AcknowledgeMessage acknowledgeMessage=new AcknowledgeMessage();
        long msisdn = portedNumber.getNumber();
        String recipientRC = portedNumber.getRecipientRC();
        String donorRC = portedNumber.getDonorRC();
        String nrhRC = portedNumber.getNrhRC();
        
        try(FileWriter fw = new FileWriter("myfile.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw))
            {
                out.println(msisdn+"\t"+recipientRC+"\t"+donorRC+"\t"+nrhRC);   
            } catch (IOException e) {
                //exception handling left as an exercise for the reader
            }

        return acknowledgeMessage;
    }
}
public class Request {

    protected List<PortedNumber> singleNumber;

    public List<PortedNumber> getSingleNumber() {
        if (singleNumber == null) {
            singleNumber = new ArrayList<PortedNumber>();
        }
        return this.singleNumber;
    }
}



public class PortedNumber {

    protected long number;
    @XmlElement(required = true)
    protected String recipientRC;
    @XmlElement(required = true)
    protected String donorRC;
    @XmlElement(required = true)
    protected String nrhRC;
    @XmlElement(required = true)
    protected PortedAction portedAction;
    protected List<ExtensionElement> extension;

    public long getNumber() {
        return number;
    }

    public void setNumber(long value) {
        this.number = value;
    }

    public String getRecipientRC() {
        return recipientRC;
    }

    public void setRecipientRC(String value) {
        this.recipientRC = value;
    }

    public String getDonorRC() {
        return donorRC;
    }
   

    public void setDonorRC(String value) {
        this.donorRC = value;
    }

    public String getNrhRC() {
        return nrhRC;
    }

    public void setNrhRC(String value) {
        this.nrhRC = value;
    }

    public PortedAction getPortedAction() {
        return portedAction;
    }

    public void setPortedAction(PortedAction value) {
        this.portedAction = value;
    } 
}

How can I get the value of getNumber() of PortedNumber from Service class?

I have tried long number = portedNumber.getNumber(); but, it wouldn't work. It stores 0 for msisdn and null for others.

I couldn't get the Arraylist values. I want to get all the values of the Arraylist and store it to a text file.

AshikAhmed
  • 33
  • 4

1 Answers1

1

You instantiate the portedNumber attribute in your Service with PortedNumber portedNumber = new PortedNumber();. This means that it has no attributes initialized and all the attributes will have their default values (null for Objects, 0 for long).

You never set those attributes and thus it is expected that It stores 0 for msisdn and null for others. as you wrote.


If I understood your question correctly you want to iterate over the PortedNumber list in request and write them to a file. If that is the case try the following:

public class Service {

    public AcknowledgeMessage checkRequestValidity(NPCHRequest request) throws IOException {
        AcknowledgeMessage acknowledgeMessage=new AcknowledgeMessage();
        
        try(FileWriter fw = new FileWriter("myfile.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw)) {
                for (PortedNumber portedNumber : request.getSingleNumber()) {
                    long msisdn = portedNumber.getNumber();
                    String recipientRC = portedNumber.getRecipientRC();
                    String donorRC = portedNumber.getDonorRC();
                    String nrhRC = portedNumber.getNrhRC();
                    out.println(msisdn+"\t"+recipientRC+"\t"+donorRC+"\t"+nrhRC); 
                }  
            } catch (IOException e) {
                //exception handling left as an exercise for the reader
            }

        return acknowledgeMessage;
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45