1

I am creating an android app that uses these classes-Graph,Vertex and Edge. Graph class has a List vertices and List edges. Vertex class has List inComing and List outGoing. vertices is a list of type Vertex, edges, inComing and outGoing of type Edge

The classes handling the android code are AddNewGroup.java and AddGroupData.java.

I make a Graph object in the AddNewGroup.java and then send it to AddGroupData.java using the cose given below:
AddNewGroup.java

 Graph g=new Graph(name,doc,desc);
            Intent intent=new Intent(this,AddGroupData.class);
            intent.putExtra("GRAPH",g);
        startActivity(intent);

AddGroupData.java

 public void onSave(View view){
        Intent intent=this.getIntent();
    g=intent.getParcelableExtra("GRAPH");

But the app crashes as soon as getParcelableExtra("GRAPH") is executed. On debugging the app I found that there is an exception of InvocationTargetException which is I think due to the Parcel being passed as a null, which is giving a NullPointException.

I tried to find on better ways to find ways to send complex objects from one activity to another, but failed. Please clarify on passing complex objects to, especially the ones which have a lot of references to other objects in form or Lists and likewise.

Graph.java

 import android.os.Parcel;
    import android.os.Parcelable;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    public class Graph<T> implements Parcelable {
        private final String name;
        private final String doc;//date of creation
        private final String desc;
        private List<Vertex> vertices;//(+)outGoing,inComing(-)
        private List<Edge> edges;//getFrom(),getTo()
        public Graph(String name,String doc,String desc){
            this.name=name;
            this.doc=doc;
            this.desc=desc;
            vertices=new ArrayList<>();
            edges=new ArrayList<>();
        }
        public List<Vertex> getVertices(){return vertices;}
        public boolean addToGraph(String from, String to, int amount,String date, String desc){
            boolean fP=false,tP=false;
            Vertex<T> f=null,t=null;
            for(Vertex<T> v:vertices){
                if((v.getName()).equals(from)){
                    fP=true;
                    f=v;
                }
                if((v.getName()).equals(to)){
                    tP=true;
                    t=v;
                }
            }
            if(fP==true && tP==true){
                boolean edgeExists=false;
                for(Edge<T> e:edges){//todo-make it iterate in f.outGoing
                    if((e.getFrom())==f &&(e.getTo())==t) {
                        e.appendCost(amount);
                        edgeExists=true;
                        return true;
                    }
                }
                if(edgeExists==false){
                    Edge<T> e=new Edge<>(f,t,amount,date,desc);
                    f.addOutGoing(e);
                    t.addIncoming(e);
                    edges.add(e);
                    return true;
                }
            }
            else if(fP==true&& tP==false){
                t = new Vertex<>(to);
                Edge<T> e=new Edge<>(f,t,amount,date,desc);
                vertices.add(t);
                edges.add(e);
                f.addOutGoing(e);
                t.addIncoming(e);
                return true;
            }
            else if(fP==false && tP==true){
                f=new Vertex<>(from);
                Edge<T> e=new Edge<>(f,t,amount,date,desc);
                vertices.add(f);
                edges.add(e);
                f.addOutGoing(e);
                t.addIncoming(e);
                return true;
            }
            else{
                f=new Vertex<>(from);
                t=new Vertex<>(to);
                Edge<T> e=new Edge<>(f,t,amount,date,desc);
                vertices.add(f);
                vertices.add(t);
                edges.add(e);
                f.addOutGoing(e);
                t.addIncoming(e);
                return true;
            }
            return false;
        }

        @Override
        public int describeContents() {
            return hashCode();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeString(doc);
            dest.writeString(desc);
            dest.writeTypedList(vertices);
            dest.writeTypedList(edges);
        }
        private Graph(Parcel p){
            name=p.readString();
            doc=p.readString();
            desc=p.readString();
            p.readTypedList(vertices,Vertex.CREATOR);
            p.readTypedList(edges,Edge.CREATOR);
        }
        public static final Parcelable.Creator<Graph> CREATOR=new Parcelable.Creator<Graph>(){

            @Override
            public Graph createFromParcel(Parcel source) {
                return new Graph(source);
            }

            @Override
            public Graph[] newArray(int size) {
                return new Graph[size];
            }
        };
        }

Vertex.java

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.List;
public class Vertex<T> implements Parcelable {
    private String name=null;
    private List<Edge> outGoing;
    private List<Edge> inComing;
    int am;
    public String getName(){
        return name;
    }
    public List<Edge> getOutGoing() {
        return outGoing;
    }
    public List<Edge> getInComing(){
        return inComing;
    }
    public boolean addOutGoing(Edge<T> e){
        outGoing.add(e);
        return true;
    }
    public boolean addIncoming(Edge<T> e){
        inComing.add(e);
        return true;
    }
    public boolean setAm(int am){this.am=am; return true;}
    public int getAm(){return am;}
    public Vertex(String name){
        this.name=name;
        outGoing=new ArrayList<>();
        inComing=new ArrayList<>();
        am=0;
    }
    public Vertex(String name, int am){
        this.name=name;
        outGoing=new ArrayList<>();
        inComing=new ArrayList<>();
        this.am=am;
    }

    @Override
    public int describeContents() {
        return hashCode();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(am);
        dest.writeTypedList(outGoing);
        dest.writeTypedList(inComing);
    }
    private Vertex(Parcel p){
        name=p.readString();
        am=p.readInt();
        p.readTypedList(outGoing,Edge.CREATOR);
        p.readTypedList(inComing,Edge.CREATOR);
    }
    public static final Parcelable.Creator<Vertex>CREATOR=new Parcelable.Creator<Vertex>(){

        @Override
        public Vertex createFromParcel(Parcel source) {
            return new Vertex(source);
        }

        @Override
        public Vertex[] newArray(int size) {
            return new Vertex[size];
        }
    };
    }

Edge.java

import android.os.Parcel;
import android.os.Parcelable;
import android.widget.EditText;
public class Edge<T> implements Parcelable{
    private Vertex<T> f;//if f is final then it will have to be instantiated by every constructor
    private  Vertex<T> t;
    private int amount;
    String desc;
    String date;
    public Vertex<T> getFrom(){
        return f;
    }
    public Vertex<T> getTo(){
        return t;
    }
    public int getAmount(){
        return amount;
    }
    public boolean appendCost(int c){
        amount+=c;
        return true;
    }
    public Edge(Vertex<T> from,Vertex<T> to, int cost,String date,String desc){
        f=from;
        t=to;
        amount=cost;
        this.desc=desc;
        this.date=date;
    }
    public Edge(Vertex<T>from, Vertex<T> to, int cost){
        f=from;
        t=to;
        amount=cost;
        desc=null;
        date=null;
    }

    @Override
    public int describeContents() {
        return hashCode();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(amount);
        dest.writeString(date);
        dest.writeString(desc);
        dest.writeParcelable(f, flags);
        dest.writeParcelable(t,flags);
    }
    private Edge(Parcel p){
        amount=p.readInt();
        date=p.readString();
        desc=p.readString();
        f=p.readParcelable(Vertex.class.getClassLoader());
        t=p.readParcelable(Vertex.class.getClassLoader());
    }
    public static final Parcelable.Creator<Edge> CREATOR=new Parcelable.Creator<Edge>(){//why cant we do Creator<Edge<T>>

        @Override
        public Edge createFromParcel(Parcel source) {
            return new Edge(source);
        }

        @Override
        public Edge[] newArray(int size) {
            return new Edge[size];
        }
    };
    }
Abhishek
  • 125
  • 8

0 Answers0