0

I'm trying to modify my first version, where it had initialized values in an array and allow dynamically inserting elements into the vector. I do this because the number of elements to manage can change.

My first version:

public ReadandWrite(SessionChannel mySession,Logger logger,List<NodeId> listaReadNodi, Connection conn) {
        this.mySession = mySession;
        this.logger = logger;
        this.conn = conn;
        
        // Create nodes array
        nodesToRead1 = new ReadValueId[]{   new ReadValueId(listaReadNodi.get(0), Attributes.Value, null, null),new ReadValueId(listaReadNodi.get(1), Attributes.Value, null, null),
                                            new ReadValueId(listaReadNodi.get(2), Attributes.Value, null, null),new ReadValueId(listaReadNodi.get(3), Attributes.Value, null, null),
                                            new ReadValueId(listaReadNodi.get(4), Attributes.Value, null, null)};
        
        cStatusPC = new CheckStatoMacchina();
        readNodesArray = new ReadNodeArray();
        
    }

Now I'm trying to rewrite in this way:

public ReadandWrite(SessionChannel mySession,Logger logger,List<NodeId> listaReadNodi, Connection conn) {
        this.mySession = mySession;
        this.logger = logger;
        this.conn = conn;
        
        // Create nodes array
        
        List<NodeId> listaNodi = new ArrayList<NodeId>();
        
        listaNodi.add(new NodeId(3, "\"Gest\".\"InProduzione\""));
        listaNodi.add(new NodeId(3, "\"Gest\".\"AusInseriti\""));
        listaNodi.add(new NodeId(3, "\"Gest\".\"InAllarme\""));     
        listaNodi.add(new NodeId(3, "\"Gest\".\"CntBuoni\""));
        listaNodi.add(new NodeId(3, "\"Gest\".\"CntScarti\""));
        
        try {
            int count=0;
        for(NodeId nodId: listaNodi) {
            
            nodesToRead1[count] = new ReadValueId(nodId, Attributes.Value, null, null);
            
            nodesToRead1 = Arrays.copyOf(nodesToRead1, count + 1);
            
        
        }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        
        cStatusPC = new CheckStatoMacchina();
        readNodesArray = new ReadNodeArray();
        
    }

But I get an Null Exception when I run the code.

java.lang.NullPointerException
    at com.ids.applicationProcessing.ReadandWrite.<init>(ReadandWrite.java:85)
    at com.ids.main.IdsMain.main(IdsMain.java:85)

and this statement retun null nodesToRead1[count] = new ReadValueId(nodId, Attributes.Value, null, null);

listaNodi isn't empty.

Is there a way to insert those elements into the array?

Scripta14
  • 463
  • 2
  • 8
  • 22

0 Answers0