Solved

Issues with casting

Posted on 2013-02-06
4
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
String array comparison 4 57
jsp insert to database example 2 90
Retrieve multiple results between delimiters in a String in Java 13 25
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

751 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