Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issues with casting

Posted on 2013-02-06
4
Medium Priority
?
434 Views
Last Modified: 2013-02-06
My users can select data real time as long as the application is open.
So I am using a WritableList with a Listener to store user selected data.  Using the WritableList allows my Tables to be updated on the fly.  So when a user adds data to the List, the listener fires and updates my tables.  Works the same for when data is removed.

The Method I am using to add the user data to the WritableList

public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
      AplotDatasetData pp = new AplotDatasetData(tcRevision, selectedDataset);
      if (!observableList.contains(pp)) {
         observableList.add(pp);
      }
   }

Open in new window


I am using the Class AplotDatasetData to format the data to add to my List
   
   ////////////////////////////////////////////////////////////////////////   
   // Start of AplotDatasetData Class                                    // 
   ////////////////////////////////////////////////////////////////////////   
   public class AplotDatasetData {

      TCComponentItemRevision rev;
      TCComponentDataset   componentdataset;
      String markUp = "no";
              
      //////////////////////////////////////////////////////////////////////////
      //                           Constructor                                //
      //////////////////////////////////////////////////////////////////////////
      public AplotDatasetData(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
         rev = tcRevision;
         componentdataset = selectedDataset;
                  
      }// end Constructor
      
      //////////////////////
      //   getDataset()   //
      //////////////////////
      public TCComponent getDataset() {
         return componentdataset;
      }
      
      //////////////////
      //   getRev()   //
      //////////////////
      public TCComponent getRev() {
         return rev;
      }

      //////////////////
      //   equals()   //
      //////////////////
      @Override
      public boolean equals(Object o) {
         AplotDatasetData p = (AplotDatasetData) o;
         if (rev.equals(p.getRev()) && componentdataset.equals(p.getDataset())) {
            return true;
         }
         else {
           return false;
          }
      }// end equals()
   }

Open in new window


The code complies.  Even when the user is adding data.  The code works great, checks for duplicate data being entered.  But when other operations are executed, it fails with the following error.

ERROR: 11:31:00,591 - TcLogger$IC_LogListener.logging:? org.eclipse.core.runtime - org.eclipse.ui - 0 - Unhandled event loop exception org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.ClassCastExc eption: org.eclipse.core.databinding.observable.list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData)
etc .....
Caused by: java.lang.ClassCastException: org.eclipse.core.databinding.observable .list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel $AplotDatasetData at com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData.equals(A plotDataModel.java:167

So what I know.

I know it is a casting issue in the equals method.
AplotDatasetData p = (AplotDatasetData) o;

Open in new window


1.  I am not sure how to change the equals method?
2.  Am I not creating the WritableList correctly?
     There are other places in the code where I need to create a new ArrayList with the data    in the WritableList
ArrayList<AplotDataModel.AplotDatasetData> tableData = new ArrayList<AplotDataModel.AplotDatasetData>(AplotDataModel.getInstance().getObservableList());

Open in new window


The above could code could be where the equals() is being called and causing the casting error.

I am looking for any help
0
Comment
Question by:jkteater
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 38860397
if (!(o instanceof AplotDatasetData)) {
  return false;
} 

Open in new window

or something to that effect should probably start your equals method
0
 

Author Comment

by:jkteater
ID: 38860488
Thank you very much!!  The comment works perfectly.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38860507
:) It must be said that you should not normally be using equals with different types. If you are, this could indicate an error
0
 

Author Comment

by:jkteater
ID: 38860578
This is the finished product.  

 @Override
      public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + getOuterType().hashCode();
         result = prime * result
               + ((componentdataset == null) ? 0 : componentdataset.hashCode());
         result = prime * result + ((rev == null) ? 0 : rev.hashCode());
         return result;
      }

Open in new window


@Override
      public boolean equals(Object obj) {
         if (this == obj)
            return true;
         if (obj == null)
            return false;
         if (getClass() != obj.getClass())
            return false;
         AplotDatasetData other = (AplotDatasetData) obj;
         if (!getOuterType().equals(other.getOuterType()))
            return false;
         if (componentdataset == null) {
            if (other.componentdataset != null)
               return false;
         }
         else if (!componentdataset.equals(other.componentdataset))
            return false;
         if (rev == null) {
            if (other.rev != null)
               return false;
         }
         else if (!rev.equals(other.rev))
            return false;
         return true;
      }

      private AplotDataModel getOuterType() {
         return AplotDataModel.this;
      }

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question