会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132834个问题

老师Scanner为什么会报空指针异常


package internet;


import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Scanner;


public class TwoSever {

      public static void main(String[] args) {

      ServerSocket serverSocket = null;

     

try {

serverSocket = new ServerSocket(8888); 

System.out.println("服务端启动,监听端口8888");

Socket socket = serverSocket.accept();

System.out.println("连接成功!");

Thread t= new Thread(new ServiceRead(socket));

Thread t1= new Thread(new ServiceWrite(socket));

t1.start();

t.start();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

}

}

}

//接收消息的

class ServiceRead implements Runnable{

    private Socket socket;

    public ServiceRead(Socket socket) {

    this.socket=socket;

    }

@Override

public void run() {

regular();

}

private void regular() {

BufferedReader bu= null;

try {

bu = new BufferedReader(new InputStreamReader(socket.getInputStream()));

   while(true) {

   String str=bu.readLine();

   System.out.println("客户端说:"+str); 

   }

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

if(bu != null) {

bu.close();

}

if(socket != null) {

socket.close();

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

}



//sever发送消息

class ServiceWrite implements Runnable{

    private Socket socket;

   

    public ServiceWrite(Socket socket) {

    this.socket=socket;

    }

@Override

public void run() {

send();

}

private void send() {

Scanner scanner = null;

PrintWriter bw =null;

try {

bw= new PrintWriter(this.socket.getOutputStream());

while(true) {

String str = scanner.nextLine();

    bw.print(str);

    bw.flush();

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

if(scanner != null) {

scanner.close();

}

if(bw != null) {

bw.close();

}

if(socket !=null) {

socket.close();

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

}


image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 33062楼

老师:

  您好,我的代码和运行结果如下:
package com.bjsxt;

public class MyDoublyLinkedList<E> implements MyList<E> {

    /**
     * 定义双向链表中的节点对象
     */
    class Node<E> {
        E item;         //记录元素
        Node<E> prev;   //记录前一个节点对象
        Node<E> next;   //记录下一个节点对象
        Node(Node<E> prev,E item, Node<E> next) {
            this.prev = prev;
            this.item = item;
            this.prev = next;
        }
    }

    private Node head;      //记录头节点
    private Node tail;      //记录尾节点
    private int size;       //记录元素个数

    /**
     * 向双向链表中添加元素
     */
    public void add(E element) {
        this.linkLast(element);
    }

    /**
     * 将节点对象添加到双向链表中的尾部
     */
    private void linkLast(E element) {
        //获取尾节点
        Node t = this.tail;

        //创建节点对象
        Node<E> node = new Node<>(t,element,null);

        //将新节点定义为尾节点
        this.tail = node;
        if(t == null) {
            this.head = node;
        }else {
            t.next = node;
        }
        this.size ++;
    }

    /**
     * 根据指定位置获取元素
     */
    public E get(int index){
        //对index进行合法性校验
        this.checkIndex(index);

        //根据位置查找节点对象
        Node<E> node = this.getNode(index);
        return node.item;
    }
    /**
     * 校验index的合法性
     */
    private void checkIndex(int index) {
        if(!(index >= 0 && index < this.size)) {
            throw new IndexOutOfBoundsException("index:"+index+"Size:"+size);
        }
    }

    /**
     * 根据位置获取指定节点对象
     */
    private Node getNode(int index) {

        //判断当前位置距离头或尾哪个更近些
        if (index < (this.size) >> 1){
            Node node = this.head;
            for(int i=0; i<index; i++) {
                node = node.next;
            }
            return node;
        }else{
            Node node = this.tail;
            for(int i = this.size-1;i>index;i--) {
                node = node.prev;
            }
            return node;
        }
    }

    /**
     * 返回元素的个数
     */
    public int size() {
        return this.size;
    }

    /**
     *  根据指定位置删除元素
     */
    public E remove(int index) {

        //对index进行合法校验
        this.checkIndex(index);

        //根据指定位置获取节点对象
        Node<E> node = this.getNode(index);

        //获取节点对象中的元素
        E item =  node.item;

        //判断节点是否为头节点
        if(node.prev == null) {
            this.head = node.next;
        }else {
            //完成当前节点直接前驱节点与当前节点的直接后继节点的挂接
            node.prev.next = node.next;
        }

        //判断节点是否为尾节点
        if(node.next == null) {
            this.tail = node.prev;
        }else{
            node.next.prev = node.prev;
        }

        //当前节点断掉与它直接前驱节点的连接
        node.prev = null;

        //当前节点断掉与它直接后继节点的连接
        node.next = null;
        node.item = null;

        //记录元素个数
        this.size--;
        return item;
    }

    public static void main(String[] args) {
        MyList<String> myList = new MyDoublyLinkedList<>();
        myList.add("a");
        myList.add("b");
        myList.add("c");
        myList.add("d");
        myList.add("e");

        System.out.println(myList.size());
        System.out.println(myList.remove(0));
        for(int i=0;i<myList.size();i++) {
            System.out.println(myList.get(i));
        }
    }
}

运行结果是:


5

a

b

c

Exception in thread "main" java.lang.NullPointerException

at com.bjsxt.MyDoublyLinkedList.get(MyDoublyLinkedList.java:59)

at com.bjsxt.MyDoublyLinkedList.main(MyDoublyLinkedList.java:150)


JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 33063楼

<style>
        body {
            background-color: #f1f1f1;
        }

        .phone {
            width: 1300px;
            height: 680px;
            margin: 0 auto;
        }

        .left {
            float: left;
            width: 234px;
            height: 680px;
            margin-top: 10px;
            background-color: pink;
        }

        .right {
            float: right;
            width: 1050px;
        }

        .item {
            width: 250px;
            height: 335px;
            float: left;
            margin-left: 10px;
            background-color: rgb(215, 212, 212);
            margin-top: 10px;
            text-align: center;
        }
        .left img {
            width: 234px;
            height: 680px;
        }
        .right img {
            width: 220px;
            height: 220px;
        }
    </style>

</head>

<body>
    <div class="phone">
        <div class="left">
            <img src="./images/1.webp">
        </div>
        <div class="right">
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
            <div class="item">
                <img src="./images/2.webp">
                <h3>黑鲨4S</h3>
                <p>磁动力升降肩键</p>
            </div>
        </div>
    </div>
</body>

image.png

WEB前端全系列/第一阶段:HTML5+CSS3模块/初识CSS 33064楼

package com.cx;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 使用封装工具
 */
public class jdbcTest2 {
    //向Department表中添加一条数据
    public void insertDeparments(String department_name,int location_id){
        Connection conn = null;
        Statement state = null;
        try {
            //使用封装工具
            conn = jdbcUtil1.getConnection();

            String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
            state = conn.createStatement();   //通过conn对象调用Connection接口下createStatement方法
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            jdbcUtil1.closeConnection(conn);
            jdbcUtil1.closeStatment(state);
        }
    }
    //更新departments表中的department_id为6的数据,将部门名称修改为教学不,location_id修改为6
    public  void  updateDepartments(String department_name,int location_id,int department_id){
        Connection conn = null;
        Statement state = null;
        try {
            conn= jdbcUtil1.getConnection();
            state = conn.createStatement();
            String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id ="+location_id+" where d.department_id ="+department_id+"";
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbcUtil1.closeConnection(conn);
            jdbcUtil1.closeStatment(state);
        }
    }
    //查询Departments表中部门ID为6的部门信息
    public void selectDepartmentsById(int departmentId){
        Connection conn = null;
        Statement state = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtil1.getConnection();
            state = conn.createStatement();
            String sql = "select * from departments d where d.department_id = "+departmentId;
            //执行查询返回结果
            rs = state.executeQuery(sql);
            while (rs.next()){
                int a = rs.getInt("department_id");
                String s = rs.getString("department_name");
                int b = rs.getInt(3);
                System.out.println(a+" "+s+" "+b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbcUtil1.closeConnection(conn);
            jdbcUtil1.closeStatment(state);
            jdbcUtil1.closeResultSet(rs);
        }
    }

    public static void main(String[] args) {
        jdbcTest2 test2 = new jdbcTest2();
        //test.insertDeparments("工程部",6);
       // test.updateDepartments("研发",6,8);
        test2.selectDepartmentsById(6);
    }
   
}
package com.cx;


import java.sql.*;
import java.util.ResourceBundle;

/**
 * 封装jdbc工具类
 */
public class jdbcUtil1 {
    private static String driver;
    private static String jdbcUrl;
    private static String username;
    private static String userpassword;
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        jdbcUrl= bundle.getString("jdbcUrl");
        username=bundle.getString("username");
        userpassword=bundle.getString("userpassword");
        try {
            Class.forName(driver);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //获取Connection对象
    public static Connection getConnection() {
        Connection conn = null;
        try {
            //获取连接
            conn = DriverManager.getConnection(jdbcUrl,username,userpassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //关闭Statement
    public static void closeStatment(Statement state){
        try {
            if (state != null){
                state.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //关闭Connection
    public static void closeConnection(Connection conn){
        try {
            if (conn != null){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //关闭
    public  static void closeResultSet(ResultSet rs){
        try {
            if (rs != null){
                rs.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
driver="com.mysql.jdbc.Driver"
jdbcUrl ="jdbc:mysql://localhost:3306/caixx?useUnicode=true&usecharacterEncoding=utf-8&useSSL=false"
username="root"
userpassword="root"


发现使用properties就会报错

image.png


不使用properties就正常, IDEA到底如何使用这个?

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 33065楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 33067楼
WEB前端全系列/第十一阶段:前端工程化/Webpack 33070楼
Python 全系列/第十四阶段:Python 爬虫开发/爬虫基础(旧) 33071楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 33073楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 33074楼



    
    Title
    
        {
            : ;
        }
        .,.,.{
            : %;
        }
        .{
            : ;
        }
        .{
            : ;
            : ;
            :;
            : ;
            : ;
        }
        .{
            : ;
        }
        .{
            : ;
        }
        .>{
            : ;
            : ;
            : ;
            : ;
            :;
            : ;

        }
        .,.,.,.{
            : ;
        }
        .>{
            : ;
            : ;
            : ;
            : ;
            : ;
        }
        .{
           : ;
            : ;
        }
        .{
            : ;
            : ;
        }
        .{
            : ;
            : ;
        }
        .{
            : ;
        }
        .{
            : ;
            : ;
            : ;
        }
        .{
            : ;
            : ;
        }
        .{
            : ;
            : ;

        }
        .{
            : ;
            : ;

        }
        .{
            : ;
            : ;

        }
        .{
            : ;
        }

    



    
        
            
                
                    18
                    200 | 
                    3999
                    
                
            
            
                
                    18 Pro
                    300 | 
                    4999
                    
                
            

        
        
            
                
                    
                    17AG
                    865 | 120 Hz
                    3699

                
            
            
            
                
                17
                865 | 120 Hz
                3699
            
            
            
                
                    
                    17 Pro
                    1200+12
                    4299
                
            
            
                
                    
                    17 Pro
                    1200+12
                    4299


WEB前端全系列/第一阶段:HTML5+CSS3模块/初识CSS 33075楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637