Home > Back-end >  How can I pass an object array to a method in another class so this method uses it as a normal array
How can I pass an object array to a method in another class so this method uses it as a normal array

Time:11-26

I am pretty new to java and programming in general so sorry if I confuse some concepts, in my code I have an object called cr which is an array in the class Menu constructed from the class ControllerRoute, this instance I require to pass to a method in the class UpdateAndDelete that does the process of update and delete of a CRUD, but the code in the method doesn't read as an array but as an object ControllerRoute. I did try having different names in the parameter and using super.cr but it didn't work for me.

The Menu is to use the different actions of the CRUD, but here is the update part, which is the one giving problems.

import javax.swing.JOptionPane;
public class Menu{
private ControllerRoute cr = new ControllerRoute(100);
UpdateAndDelete ud = new UpdateAndDelete();

   public void updateRoute(){
      ControllerRoute ur = new ControllerRoute(0);
      int id;
      id = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter an Id"));
      ud.updateRoutes(id, cr);
      }
}

Route alongside ControllerRoute are the classes used to create the array and from where the object cr comes.

public class Route{
   private int id;
   private String name;
   
   public Route () {}
   
   public Route(int id, String name){
      this.id = id;
      this.name = name;
   }
}
public class ControllerRoute extends Menu{
   Route[] routes;
   
   public ControllerRoute(int size){
      routes = new Route[size];
   }
}

UpdateAndDelete is the method that is asking for cr to do the process of update and delete parts of the array. Here is where in every place that has cr in it an error appears because either it can't found the symbol nor can recognize cr as an array.

import javax.swing.JOptionPane;
public class UpdateAndDelete{

   public Route updateRoutes(int id, ControllerRoute cr){
      int pos = -1;
      String nwname;
      for(int i=0; i<cr.length; i  ){
           if(id == cr[i].getId()){
            pos=i;
           }
      }
      return null;
   }
}

Errors found:

error: cannot find symbol on the line: for(int i=0; i<cr.length; i ){

UpdateAndDelete.java:14: error: array required, but ControllerRoute found in the lines: if(id == cr[i].getId()){

cr[pos] = new Route(id, nwname);

I first tried changing the parameter, either the name of the array or the type alongside it between int, String, Array. Route[] and the final one being ControllerRoute.

After that I tried using super.cr to see if I could take it directly from menu, but it gave problems with methods like .length

import javax.swing.JOptionPane;
public class UpdateAndDelete extends Menu{

   public Route updateRoutes(int id, ControllerRoute cr){
      int pos = -1;
      String nwname;
      for(int i=0; i<super.cr.length; i  ){
           if(id == super.cr[i].getId()){
            pos=i;
           }
      }
      return null;
   }
}

This gives the same errors.

Other thing that I tried with the super was trying create another array in UpdateAndDelete and use super.cr to translate it's value to it (example: pj = super.cr), but it gave out the error that the array wasn't compatible with ControllerRoute

CodePudding user response:

ControllerRoute is not an array. it has an attribute which is array. try to write a getter for routes in ControllerRoute and use that to get the array. ud.updateRoutes(id, cr.getRoutes());

  • Related