1.出错日志 Caused by: java.lang.NullPointerException: charsetName 
at java.lang.String.(String.java:403) 
at jxl.biff.StringHelper.getString(StringHelper.java:164) 
at jxl.biff.FormatRecord.(FormatRecord.java:161) 
at jxl.read.biff.WorkbookParser.parse(WorkbookParser.java:638) 
at jxl.Workbook.getWorkbook(Workbook.java:237) 
at jxl.Workbook.getWorkbook(Workbook.java:198) 
at com.ppet.inv.BalanceUpdateAction.execute(BalanceUpdateAction.java:106) 
... 36 more 
2. BalanceUpdateAction.execute:代码截图 
----------------------
这个是excel更新库存时的出错信息……  
请问,出现这个错误的原因可能是什么呢?

解决方案 »

  1.   

    查看了下源码,getWorkbook(File) 会创建一个默认的WorkbookSettings 对象,这个设置对象默认的encoding是由 System.getProperty("file.encoding")获得的。
      public WorkbookSettings()
      {
        initialFileSize = DEFAULT_INITIAL_FILE_SIZE;
        arrayGrowSize = DEFAULT_ARRAY_GROW_SIZE;
        localeFunctionNames = new HashMap();
        excelDisplayLanguage = CountryCode.USA.getCode();
        excelRegionalSettings = CountryCode.UK.getCode();
        refreshAll = false;
        template = false;
        excel9file = false;
        windowProtected = false;
        hideobj = HIDEOBJ_SHOW_ALL;    // Initialize other properties from the system properties
        try
        {
          boolean suppressWarnings = Boolean.getBoolean("jxl.nowarnings");
          setSuppressWarnings(suppressWarnings);
          drawingsDisabled        = Boolean.getBoolean("jxl.nodrawings");
          namesDisabled           = Boolean.getBoolean("jxl.nonames");
          gcDisabled              = Boolean.getBoolean("jxl.nogc");
          rationalizationDisabled = Boolean.getBoolean("jxl.norat");
          mergedCellCheckingDisabled =
            Boolean.getBoolean("jxl.nomergedcellchecks");
          formulaReferenceAdjustDisabled =
                                    Boolean.getBoolean("jxl.noformulaadjust");
          propertySetsDisabled = Boolean.getBoolean("jxl.nopropertysets");
          ignoreBlankCells = Boolean.getBoolean("jxl.ignoreblanks");
          cellValidationDisabled = Boolean.getBoolean("jxl.nocellvalidation");
          autoFilterDisabled = !Boolean.getBoolean("jxl.autofilter"); 
                 // autofilter currently disabled by default
          useTemporaryFileDuringWrite = 
            Boolean.getBoolean("jxl.usetemporaryfileduringwrite");
          String tempdir =
            System.getProperty("jxl.temporaryfileduringwritedirectory");      if (tempdir != null)
          {
            temporaryFileDuringWriteDirectory = new File(tempdir);
          }
          //在这里设置的
          encoding = System.getProperty("file.encoding");
        }
        catch (SecurityException e)
        {
          logger.warn("Error accessing system properties.", e);
        }    // Initialize the locale to the system locale
        try
        {
          if (System.getProperty("jxl.lang")    == null ||
              System.getProperty("jxl.country") == null)
          {
            locale = Locale.getDefault();
          }
          else
          {
            locale = new Locale(System.getProperty("jxl.lang"),
                                System.getProperty("jxl.country"));
          }      if (System.getProperty("jxl.encoding") != null)
          {
            encoding = System.getProperty("jxl.encoding");
          }
        } 
        catch (SecurityException e)
        {
          logger.warn("Error accessing system properties.", e);
          locale = Locale.getDefault();
        }
      }
    StringHelper使用的就是这个encoding:  public static String getString(byte[] d, int length, int pos,
                                     WorkbookSettings ws)
      {
        if( length == 0 )
        {
          return "";  // Reduces number of new Strings
        }    try
        {
          return new String(d, pos, length, ws.getEncoding());
          //      byte[] b = new byte[length];
          //      System.arraycopy(d, pos, b, 0, length);
          //      return new String(b, ws.getEncoding());
        }
        catch (UnsupportedEncodingException e)
        {
          logger.warn(e.toString());
          return "";
        }
      }
    查看下System.getProperty("file.encoding")获得的系统编码是否正确。
      

  2.   

    另外,JXL的配置里面你是否把encoding配置错误了,上面贴的第一段构造方法的代码中,encoding的设置还会被你的JXL配置覆盖掉。
        // Initialize the locale to the system locale
        try
        {
          if (System.getProperty("jxl.lang")    == null ||
              System.getProperty("jxl.country") == null)
          {
            locale = Locale.getDefault();
          }
          else
          {
            locale = new Locale(System.getProperty("jxl.lang"),
                                System.getProperty("jxl.country"));
          }      if (System.getProperty("jxl.encoding") != null)
          {
            //这里覆盖
            encoding = System.getProperty("jxl.encoding");
          }
        } 
        catch (SecurityException e)
        {
          logger.warn("Error accessing system properties.", e);
          locale = Locale.getDefault();
        }