好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

java中常见错误和异常解决方法(打印).doc

41页
  • 卖家[上传人]:第***
  • 文档编号:31077526
  • 上传时间:2018-02-04
  • 文档格式:DOC
  • 文档大小:873.50KB
  • / 41 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1.java.lang.NullPointerException原因是:有空指针,有地址没赋值2.Exception in thread "main" java.lang.ArithmeticException: / by zero原因是除数是 03.ArrayIndexOutOfBoundsException原因是:数组越界4.java.lang.NumberFormatException原因是:数字格式化有问题5.Unhandled exception type Exception原因是:没有进行异常处理6.进行国际化操作的时候遇到这样的错误:Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Message, locale zh_CN答:因为在命令提示符中,是没有错误的解决方法是:在 myeclipse 中,会出现这个错误java 国际化之 Can't find bundle for base name1.初步学习最近在学习 ResourseBundle 时遇到了“Can't find bundle for base name ”这个错误搞了很久才解决了。

      原因就是类路径问题要将属性文件放在类路径中!百度里很多都是教程但没有涉及到解决方法! 2.中文显示:测试文件java 代码package com.lht.ResourseBundleStudy;import java.util.ResourceBundle;public class ResourseBundleDemo {public static void main(String[] args) {ResourceBundle resource = ResourceBundle.getBundle("test");System.out.print(resource.getString("msg0") + "!");System.out.println(resource.getString("msg1") + "!"); }}test.propertiesmsg0="Hello World"msg1="da jia hao"开始自己测试的时候:将属性文件放在 bin/下也试过也不行无赖中就在 google 中搜索了一下终于在 sun 的 java 论坛( solved the problem the best way possible. Basically what i've done is added a new class folder named config to the project home dir. Then i added this classfolder to the classpath in project properties. After doing all of this you only need to reference the properties file by "Email".Hope this helps anyone else who is having similiar problems.基本意思就是在 src 下建立 classes(名字无所谓) 文件夹将属性文件存放在下面,然后将这个文件夹加入类路径中!运行就可以了:加入类路径的方法:你的工程文件夹 ->properties->选择 Libraries 选项卡->Add Class Folder将刚才建立的文件夹加入就可以了!结果如下:"Hello World"!"da jia hao";!2.中文显示在 classes 目录下建立 message_CH.properties 内容如下 :ms0="大家好"同样用上面的测试文件!结果如下:"?ó????"!乱码怎么回事啊!在百度里搜索后找到了答案有以为网友写的很清楚: 文件中,使用的编码方式根据机器本身的设置可能是 GBK 或者 UTF-8。

      而在Java 程序中读取 Property 文件的时候使用的是 Unicode 编码方式,这种编码方式不同会导致中文乱码因此需要将 Property 文件中的中文字符转化成 Unicode 编码方式才能正常显示中文解决办法:Java 提供了专门的工具对 Property 文件进行 Unicode 转化,这种工具就是 native2ascii,它在 JDK 安装环境的 bin 目录下native2ascii 工具将带有本机编码字符(非拉丁 1 和非单一码字符)的文件转换成带有Unicode 编码字符的文件假设需要转化的属性文件为:D:/src/resources.properties (含有中文字符)转化后的属性文件为:D:/classes/resources.properties(中文字符统一转化为 Unicode)那么使用如下命令JAVA_HOME/bin/native2ascii -encoding GBK D:/src/resources.properties D:/classes/resources.properties就能将含有中文字符的属性文件转化成单一 Unicode 编码方式的属性文件。

      中文乱码自然会被解决通过上面的方法我将生成的文件打开一看内容如下:ch="/u5927/u5bb6/u597d"再运行结果如下:"大家好"(2 )另一种解决办法:Can't find bundle for base nameStruts2国际化异常处理这是找不到指定文件;你必须把 .properties 文件,放在与这个调用文件 .java 相同的目录里;Hello.java 在 workspace\test\src\com\lj\guojiehua 下hello_en_US.properties 必须在 workspace\test\src 下hello_zh_CN.properties 必须在 workspace\test\src 下其实原因是我虽然在 build path 里面 加了\pruway\source\source\config,但是系统编译的时候,在 classes 里面应该会自动产生 resource_en_US.properties,可是实际情况是classes 包下面没有产生,故我删掉重加,再编译结果发现通过了 就是说,只要你buildpath 路径对了,该路径下也有 resoucebudle 需要的类,那么系统会自动在 classes 里面自动编译产生这些类的。

      所以,先检查 classes 里,有没有生成对应的resource_en_US.properties,如果没有,那么检查 build path 路径下有没有对应的properties 类,如果有,那么证明系统编译没有编译完整,删掉 path,重新 add foler,加入,再编译,检查 classes 下有无 如果都有,那么证明成功 10.java.lang.IllegalThreadStateException解决办法:不能启动两次线程11.java.lang.NoSuchMethodError答:必须有一个 public static void main(String[] args){ //这里是入口 } 作为入口点,启动 java 虚拟机时虚拟机会找这个方法,如果没有就报 exception in thread “main”.UnknownHostException: .InetAddress ;public class a{public static void main(String args[]) throws Exception {// 所有异常抛出InetAddress locAdd = null ;InetAddress remAdd = null ;locAdd = InetAddress.getLocalHost() ;// 得到本机remAdd = InetAddress.getByName(" ;System.out.println("本机的IP 地址:" + locAdd.getHostAddress()) ;System.out.println("MLDNJAVA的IP地址:" + remAdd.getHostAddress()) ;System.out.println("本机是否可达:" + locAdd.isReachable(5000)) ;}//5000代表代数};运行后结果为: 如果注释掉 remAdd = InetAddress.getByName(" ;运行结果又是这样的:上述程序的解决办法是:将remAdd = InetAddress.getByName(" ;改成程序运行结果如下:完成实验要求,但是这里就有一个问题了,为什么去掉 http//和后面的/的就运行的了呢?(2)但是对于这个极其相似的程序却可以运行出结果:import .InetAddress ;public class b {public static void main(String args[])throws Exception{InetAddress address=InetAddress.getByName("");System.out.println("ip: "+address.getHostAddress());System.out.println("host: "+address.getHostName()); System.out.println("canonical host name: "+address.getCanonicalHostName());byte[] bytes=address.getAddress();for(byte b:bytes){if(b>=0)System.out.print(b);else System.out.print(256+b);}}}运行结果为:13.运行下列程序出现了这样的错误,这是线程的知识,一个线程如果启动了,再启动一个的时候就会报错:java.lang.IllegalThreadStateException程序如下:class thrund extends Thread{private String name;public thrund(String name){this.name=name;}public void run(){ //线程完成的动作for(int j=0;j{private T y;public T p(T y){System.out.println(y);return y;}}public class a{public static void main (String args[]){add adder=new add();//System.out.println("输出属性"+adder.getX());//adder。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.