Java实操可能出现的错误 1

1 空指针

在这里插入图片描述

package com.imooc.java.escape;

/**
 * <h1>理解什么是空指针</h1>
 * */
public class WhatIsNpe {
   

    public static class User {
   

        private String name;
        private String[] address;

        public void print() {
   
            System.out.println("This is User Class!");
        }

        public String readBook() {
   
            System.out.println("User Read Imooc Escape!");
            return null;
        }
    }

    /**
     * <h2>自定义一个运行时异常</h2>
     * */
    public static class CustomException extends RuntimeException {
   }

    public static void main(String[] args) {
   

        // 第一种情况: 调用了空对象的实例方法
//        User user = null;
//        user.print();

        // 第二种情况: 访问了空对象的属性
//        User user = null;
//        System.out.println(user.name);

        // 第三种情况: 当数组是一个空对象的时候, 取它的长度
//        User user = new User();
//        System.out.println(user.address.length);

        // 第四种情况: null 当做 Throwable 的值
//        CustomException exception = null;
//        throw exception;

        // 第五种情况: 方法的返回值是 null, 调用方直接去使用
        User user = new User();
        System.out.println(user.readBook().contains("MySQL"));
    }
}

在这里插入图片描述

1.1自动拆箱引发的空指针问题

package com.imooc.java.escape;

/**
 * <h1>自动拆箱引发的空指针问题</h1>
 * */
@SuppressWarnings("all")
public class UnboxingNpe {
   

    private static int add(int x, int y) {
   
        return x + y;
    }

    private static boolean compare(long x, long y) {
   
        return x >= y;
    }

    public static void main(String[] args) {
   

        // 1. 变量赋值自动拆箱出现的空指针
        // javac UnboxingNpe.java
        // javap -c UnboxingNpe.class
        Long count = null;
        long count_ = count;

        // 2. 方法传参时自动拆箱引发的空指针
//        Integer left = null;
//        Integer right = null;
//        System.out.println(add(left, right));

        // 3. 用于大小比较的场景
//        Long left = 10L;
//        Long right = null;
//        System.out.println(compare(left, right));
    }
}

1.2字符串、数组、集合在使用时出现空指针

在这里插入图片描述

package com.imooc.java.escape;

import java.util.ArrayList;
import java.util.List;

/**
 * <h1>字符串、数组、集合在使用时出现空指针</h1>
 * */
@SuppressWarnings("all")
public class BasicUsageNpe {
   

    private static boolean stringEquals(String x, String y) {
   
        return x.equals(y);
    }

    public static class User {
   
        private String name;
    }

    public static void main(String[] args) {
   

        // 1. 字符串使用 equals 可能会报空指针错误
//        System.out.println(stringEquals("xyz", null));
//        System.out.println(stringEquals(null, "xyz"));

        // 2. 对象数组 new 出来, 但是元素没有初始化
//        User[] users = new User[10];
//        for (int i = 0; i != 10; ++i) {
   
//            users[i] = new User();
//            users[i].name = "imooc-" + i;
//        }

        // 3. List 对象 addAll 传递 null 会抛出空指针
        List<User> users = new ArrayList<User>();
        User user = null;
        List<User> users_ = null;

        users.add(user);
        users.addAll(users_);
    }
}

2异常

在这里插入图片描述

2.1 常见的异常

并发修改异常:遍历的同时做删除操作,会出现并发修改异常。用迭代器
类型转换异常

package com.imooc.java.escape;

import com.google.common.base.Enums;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <h1>编码中的常见的异常</h1>
 * */
@SuppressWarnings("all")
public class GeneralException {
   

    public static class User {
   

        private String name;

        public User() {
   }

        public User(String name) {
   
            this.name = name;
        }

        public String getName() 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值