Solved

Issues with casting

Posted on 2013-02-06
4
419 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now