簡體   English   中英

當我進行編譯時,它顯示有關Product.java的錯誤。 但該文件正在工作並顯示

[英]When i am compiling it it shows a errors regarding Product.java. but that file is working and displayed

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class MyHandler extends DefaultHandler {

//List to hold Employees object
public HashMap<String,Product> prodList = null;

public Product product = null;

//getter method for employee list
public HashMap<String,Product> getEmpList() {
    return prodList;
}

boolean bImage = false;
boolean bName = false;
boolean bAccessories = false;
boolean bCondition = false;
boolean bPrice = false;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {

    if (qName.equalsIgnoreCase("watch" )) {
        //create a new Employee and put it in Map
        String id = attributes.getValue("id");
        //initialize Employee object and set id attribute
        product = new Product();
        product.setId(id);
        String retailer = attributes.getValue("retailer");
        //initialize Employee object and set id attribute
        product.setRetailer(retailer);
        product.setProducttype(qName);
        //initialize list
        if (prodList == null)
            prodList = new HashMap<String,Product>();
    } 
    else if (qName.equalsIgnoreCase("speaker" )) {
        //create a new Employee and put it in Map
        String id = attributes.getValue("id");
        //initialize Employee object and set id attribute
        product = new Product();
        product.setId(id);
        String retailer = attributes.getValue("retailer");
        //initialize Employee object and set id attribute
        product.setRetailer(retailer);
        product.setProducttype(qName);
        //initialize list
        if (prodList == null)
            prodList = new HashMap<String,Product>();
    } 
     else if (qName.equalsIgnoreCase("laptop" )) {
        //create a new Employee and put it in Map
        String id = attributes.getValue("id");
        //initialize Employee object and set id attribute
        product = new Product();
        product.setId(id);
        String retailer = attributes.getValue("retailer");
        //initialize Employee object and set id attribute
        product.setRetailer(retailer);
        product.setProducttype(qName);
        //initialize list
        if (prodList == null)
            prodList = new HashMap<String,Product>();
    } 
      else if (qName.equalsIgnoreCase("headphone" )) {
        //create a new Employee and put it in Map
        String id = attributes.getValue("id");
        //initialize Employee object and set id attribute
        product = new Product();
        product.setId(id);
        String retailer = attributes.getValue("retailer");
        //initialize Employee object and set id attribute
        product.setRetailer(retailer);
        product.setProducttype(qName);
        //initialize list
        if (prodList == null)
            prodList = new HashMap<String,Product>();
    } 
    else if (qName.equalsIgnoreCase("phone" )) {
        //create a new Employee and put it in Map
        String id = attributes.getValue("id");
        //initialize Employee object and set id attribute
        product = new Product();
        product.setId(id);
        String retailer = attributes.getValue("retailer");
        //initialize Employee object and set id attribute
        product.setRetailer(retailer);
        product.setProducttype(qName);
        //initialize list
        if (prodList == null)
            prodList = new HashMap<String,Product>();
    } 
       else if (qName.equalsIgnoreCase("storage" )) {
        //create a new Employee and put it in Map
        String id = attributes.getValue("id");
        //initialize Employee object and set id attribute
        product = new Product();
        product.setId(id);
        String retailer = attributes.getValue("retailer");
        //initialize Employee object and set id attribute
        product.setRetailer(retailer);
        product.setProducttype(qName);
        //initialize list
        if (prodList == null)
            prodList = new HashMap<String,Product>();
    }

    else if (qName.equalsIgnoreCase("name")) {
        //set boolean values for fields, will be used in setting Employee variables
        bName = true;
    } else if (qName.equalsIgnoreCase("image")) {
        bImage = true;
    } else if (qName.equalsIgnoreCase("accessories")) {
        bAccessories = true;
    }else if (qName.equalsIgnoreCase("condition")) {
        bCondition = true;
    } else if (qName.equalsIgnoreCase("price")) {
        bPrice = true;
    } 
     }

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equalsIgnoreCase("watch"))
            {
        //add Product object to list
        prodList.put(product.getId(),product);
    }
    else if(qName.equalsIgnoreCase("speaker"))
            {
        //add Product object to list
        prodList.put(product.getId(),product);
            }
             else if(qName.equalsIgnoreCase("laptop"))
            {
        //add Product object to list
        prodList.put(product.getId(),product);
            }
             else if(qName.equalsIgnoreCase("headphone"))
            {
        //add Product object to list
        prodList.put(product.getId(),product);
            }
             else if(qName.equalsIgnoreCase("phone"))
            {
        //add Product object to list
        prodList.put(product.getId(),product);
            }
             else if(qName.equalsIgnoreCase("storage"))
            {
        //add Product object to list
        prodList.put(product.getId(),product);
            }
    }

@Override
public void characters(char ch[], int start, int length) throws SAXException {
    if (bImage) {
        //age element, set Employee age
        product.setImage(new String(ch, start, length));
        bImage = false;
    } else if (bName) {
        product.setName(new String(ch, start, length));
        bName = false;
    } else if (bCondition) {
        product.setCondition(new String(ch, start, length));
        bCondition = false;
    } else if (bPrice) {
        product.setPrice(Double.parseDouble(new String(ch, start, length)));
        bPrice = false;
    }
}
}

問題是無法將Product識別為類型。 它發生的原因很可能class Product是不是在同一個包MyHandler類。 因此,有兩種處理方法:

  1. Product類與MyHandler類放在同一包中
  2. 導入Product類。 假設它在mypackage軟件包中。 然后,您需要添加:

    import mypackage.Product;

MyHandler類的頂部。


如果Product 在同一個包,前mypackage ,那么你需要在你的頂部添加此行MyHandler類以及在Product類:

package mypackage;


希望對您有所幫助!

看一下您的import語句,沒有這樣的類已經被導入了,所以首先在MyHandeler中導入Product類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM