0

i have a combobox.it has category name.so i need to pass string to category. Category category = (Category)cmbCategory.getSelectedItem(); this line get error saying cant cast string to category type..

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    double amount = Double.parseDouble(txtAmount.getText());
    Expense expense = new Expense();
    expense.setReason(txtReason.getText());
    expense.setAmount(amount);
    DateFormat format = new SimpleDateFormat("MMMM d, yyyy");

    Category category = (Category)cmbCategory.getSelectedItem();
    String startDate = ((JTextField)txtDate.getDateEditor().getUiComponent()).getText();

    try {
        Date date = format.parse(startDate);
        expense.setDate(date);
    } catch (ParseException ex) {
        Logger.getLogger(ExpenseView.class.getName()).log(Level.SEVERE, null, ex);
    }

    expense.setCategory(category);
    ExpenseController.saveExpense(expense);
}          
5
  • is the type of cmbCategory.getSelectedItem(); String? Commented Sep 8, 2016 at 18:37
  • yes.i pass it as string Commented Sep 8, 2016 at 18:41
  • 1
    Well then that is you're problem. You are trying to tell the compiler that getSelectedItem() is going to give you a Category or subclass of Category, which String is not. Commented Sep 8, 2016 at 18:43
  • You might consider implementing some kind of Category.forName(String) method, which returns a Category based on the given String. Commented Sep 8, 2016 at 18:45
  • i need to pass it as category object to model->expense.java class.model->expense.java class has getter which accept category object.i need to pass as object including both valuemember and displaymember (name and id) of combo Commented Sep 8, 2016 at 18:53

2 Answers 2

1

It looks like you are trying to convert and then assign a String to a Category.

Since String is not a Category obviously, nor implementing Category, you receive this exception.

You can add your Category class a string data member such as categoryNameand then set it as follows :

Category category = new Category().setCategoryName(cmbCategory.getSelectedItem());
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for help.it occured bcz i was trying to cast an instance of an Object to a type that it is not. now it ok
glad to hear, accept the answer for the future people looking at this question :)
i passed category object to comboadditem methode cmbCategory.addItem(category);.error solved.but when i run and looking at combobox it disappear name.i know problem is i passed category object.but category object has name and id.how i can display name in combo.
Can you provide a working example in Plnkr/JSFiddle? and as long as this post is solved please do open a new post for any further non-related questions
May I ask why did you took the accept down? as it does answer the question
1

Category.getSelectedItem (); returns a String

unless it is assigned to an attribute of your class Category for example name and is of type string

category.setName(cmbCategory.getSelectedItem());

1 Comment

thanks for help.it occured bcz i was trying to cast an instance of an Object to a type that it is not. now it ok

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.