java.lang.NullPointerException:无法调用Controller.addFlight()”,因为Controller为null

hjzp0vay  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(114)

我遇到了一个空指针异常。你能帮我解释一下实际问题是什么,我做错了什么。原谅我的编码粗糙,因为我是初学者。
这是错误:
线程“main”java.lang.NullPointerException:无法调用“com.org.Controller.Controller.addFlight()”,因为“com.org.App.controller”在com.org.App.main(App.java:22)中为空。
这些是类:

应用程序

package com.org;

import com.org.Config.AppConfig;
import com.org.Controller.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Scanner;

public class App {

    @Autowired
    public static Controller controller;

        public static void main(String[] args) {
            System.out.println("Hello, Welcome to AirGO Airline");
            System.out.println("Please chose what you would like to do");
            System.out.println("1.Add Flight:");
            System.out.println("2.Search Flight");
            Scanner scanner=new Scanner(System.in);
            Integer userChoice= scanner.nextInt();
            if (1==userChoice){
                controller.addFlight();
            } else if (2==userChoice) {
                controller.showFlight();
            }
            else
            System.out.println("Please Enter a Valid Input");
        }
}

Controller//一个伪控制器类

package com.org.Controller;

import com.org.Config.AppConfig;
import com.org.Domain.Flight;
import com.org.Service.FlightServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
@Component
public class Controller {

    @Autowired
    FlightServiceImpl flightService= new FlightServiceImpl();
    public void addFlight(){
new AnnotationConfigApplicationContext(AppConfig.class);
        Flight flight=new Flight();
        flight.setAirlines("Indigo");
        flight.setSource("India");
        flight.setDestination("Japan");
        flight.setFare(50456.54);
        LocalDate date= LocalDate.now();
        flight.setJourneyDate(date);
        flight.setSeatCount(2);
        flightService.addFlight(flight);
    }

    public void showFlight(){
        ApplicationContext applicationContext= new AnnotationConfigApplicationContext(AppConfig.class);
        FlightServiceImpl flightService= applicationContext.getBean(FlightServiceImpl.class);
        LocalDate date= LocalDate.now();
//not complete
    }
}

服务项目

package com.org.Service;

import com.org.Domain.Flight;
import com.org.Repository.FlightRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FlightServiceImpl implements FlightService{

    @Autowired
    FlightRepositoryImpl flightRepository;

    @Override
    public void addFlight(Flight flight) {
        try{
            flightRepository.addFlight(flight);
        }catch (Exception e){
            System.out.println("Flight Insertion Unsuccessful, Cannot connect to Database....");
        }

    }
}

配置类

package com.org.Config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.org")
public class AppConfig {
    //Beans AutoGenerated
}

域名类

package com.org.Domain;

import java.time.LocalDate;

public class Flight {
    private String flightId;
    private String airlines;
    private String source;
    private String destination;
    private double fare;
    private LocalDate journeyDate;
    private Integer seatCount;

    public Flight() {
    }

    public String getFlightId() {
        return flightId;
    }

    public void setFlightId(String flightId) {
        this.flightId = flightId;
    }

    public String getAirlines() {
        return airlines;
    }

    public void setAirlines(String airlines) {
        this.airlines = airlines;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public double getFare() {
        return fare;
    }

    public void setFare(double fare) {
        this.fare = fare;
    }

    public LocalDate getJourneyDate() {
        return journeyDate;
    }

    public void setJourneyDate(LocalDate journeyDate) {
        this.journeyDate = journeyDate;
    }

    public Integer getSeatCount() {
        return seatCount;
    }

    public void setSeatCount(Integer seatCount) {
        this.seatCount = seatCount;
    }

    @Override
    public String toString() {
        return "Flight{" +
                "flightId='" + flightId + '\'' +
                ", airlines='" + airlines + '\'' +
                ", source='" + source + '\'' +
                ", destination='" + destination + '\'' +
                ", fare=" + fare +
                ", journeyDate=" + journeyDate +
                ", seatCount=" + seatCount +
                '}';
    }
}

寄存处

package com.org.Repository;

import com.org.Domain.Flight;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class FlightRepositoryImpl implements FlightRepository{

    private static Integer flightId = 1000;
    @Override
    public void addFlight(Flight flight) {
        Map<Integer, Flight> database= new HashMap<Integer, Flight>();
        flightId+=1;
        database.put(flightId,flight);
        System.out.println("Added flight details are: "+ flight.toString());
    }
}
nimxete2

nimxete21#

更新你的配置类如下。

package com.org.Config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.org")
public class AppConfig {
    //Beans AutoGenerated

    @Bean
    public Controller controller() {
       return new Controller();
    }
}

但在理想世界里。控制器类应该使用@RestController@Controller进行注解,并Map到正确的http端点Map。
希望这对你有帮助。
更新:如果使用Spring-Boot,您的App类也缺少@SpringBootApplication,如果使用Spring,则缺少@Component。根据用例更新您的类。
Sping Boot :

@SpringBootApplication
public class App {

    @Autowired
    public static Controller controller;

        public static void main(String[] args) { 
           SpringApplication.run(args, App.class);
        }
}

Spring:

@Component
public class App {

    public static Controller controller;

        public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
            controller = context.getBean(Controller.class);
            System.out.println("Hello, Welcome to AirGO Airline");
            System.out.println("Please chose what you would like to do");
            System.out.println("1.Add Flight:");
            System.out.println("2.Search Flight");
            Scanner scanner=new Scanner(System.in);
            Integer userChoice= scanner.nextInt();
            if (1==userChoice){
                controller.addFlight();
            } else if (2==userChoice) {
                controller.showFlight();
            }
            else
            System.out.println("Please Enter a Valid Input");
        }
}

我更喜欢Sping Boot 。如果你想在基于命令行的应用程序上获得进一步的帮助,请在GitHub上创建一个包含示例代码的存储库,并在这里分享,我们可以讨论和贡献。

相关问题