NullPointerException on appium service

I am running a few classes together using Appium. The first class starts appium service fine, but second class onwards I get a nullPointerException. This is happening consistently:

[INFO ] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 98 initialize - BeforeClass from NaluTestSection
[INFO ] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 357 stopAppiumService - Appium service stopped...
[INFO ] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 324 startAppiumService - Starting Appium service. 
 Logs will be stored at: .\Outputs\AppiumLogs\TP397_Procedure2\appium_TP397_Procedure2_Section01.log
[ERROR] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 338 startAppiumService - Error starting Appium service...
[INFO ] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 340 startAppiumService - Retrying Appium service start
[ERROR] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 345 startAppiumService - Error starting Appium service...: full stack trace follows:
java.lang.NullPointerException: null
	at framework.testWriting.NaluTestSection.startAppiumService(NaluTestSection.java:342) ~[test-classes/:?]
	at framework.testWriting.NaluTestSection.initialize(NaluTestSection.java:104) ~[test-classes/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_261]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_261]
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133) ~[org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62) ~[org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385) ~[org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321) ~[org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176) ~[org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122) ~[org.testng_7.4.0.r202105021533.jar:7.4.0]
	at java.util.ArrayList.forEach(Unknown Source) [?:1.8.0_261]
	at org.testng.TestRunner.privateRun(TestRunner.java:794) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.TestRunner.run(TestRunner.java:596) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:377) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.SuiteRunner.run(SuiteRunner.java:276) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1134) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.TestNG.runSuites(TestNG.java:1063) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.TestNG.run(TestNG.java:1031) [org.testng_7.4.0.r202105021533.jar:7.4.0]
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) [testng-remote.jar:?]
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) [testng-remote.jar:?]
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77) [testng-remote.jar:?]
[INFO ] 2022-02-17 15:59:32 framework.testWriting.NaluTestSection 129 initialize - Starting Appium driver...
[INFO ] 2022-02-17 15:59:34 framework.testWriting.NaluTestSection 140 initialize - Retrying Appium driver start...
[ERROR] 2022-02-17 15:59:36 framework.testWriting.NaluTestSection 143 initialize - Error starting Appium driver...org.openqa.selenium.WebDriverException: Connection refused: connect
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'NALU-PF2JMR3R', ip: '192.168.100.126', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_261'
Driver info: driver.version: AppiumDriver
[INFO ] 2022-02-17 15:59:36 framework.testWriting.NaluTestSection 166 setUpTest - BeforeMethod from NaluTestSection
[INFO ] 2022-02-17 15:59:36 framework.testWriting.NaluTestSection 215 setUpTest - Testname in NaluTestSection is: TP397_060
[INFO ] 2022-02-17 15:59:36 framework.testWriting.NaluTestSection 219 setUpTest - Relaunching app due to setup incomplete or driver==null...
[INFO ] 2022-02-17 15:59:36 framework.commonUtilities.ExtentReports.ExtentListener 194 beforeInvocation - Test started - from extent reports
[INFO ] 2022-02-17 15:59:36 framework.commonUtilities.ExtentReports.ExtentListener 201 beforeInvocation - TestName in extentListener is: TP397_060
[INFO ] 2022-02-17 15:59:36 framework.Listeners.ExecutionListener 145 beforeInvocation - BeforeInvocation: TP397_060
[INFO ] 2022-02-17 15:59:36 framework.Listeners.ExecutionListener 150 beforeInvocation - 

My code is as follows:

	@Parameters({"deviceName","platformVersion", "portNumber"})
	@BeforeClass
	public void initialize( String deviceName, String platformVersion, String portNumber) throws MalformedURLException, WebDriverException{
		try {
				log.info("BeforeClass from NaluTestSection");
				//Start Appium Service
				if(checkIfServiceIsRunning(Integer.parseInt(portNumber)))
					stopAppiumService();
				
				try {
					startAppiumService(portNumber, this.getClass().getSimpleName());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				//Set Appium driver capabilities
				loadPropFile = new LoadPropertiesFile(System.getProperty("user.dir")  + "\\framework.properties");
				loadPropFile.loadPropertiesFile();
				caps = new DesiredCapabilities();

				caps.setCapability("platformName", loadPropFile.getProperty("appium.platformName"));
				caps.setCapability("newCommandTimeout", loadPropFile.getProperty("appium.newCommandTimeout"));
				caps.setCapability("resetKeyboard", loadPropFile.getProperty("appium.resetKeyboard"));
				caps.setCapability("autoGrantPermissions", loadPropFile.getProperty("appium.autoGrantPermissions"));
				caps.setCapability("disableAndroidWatchers", true);
				caps.setCapability("skipUnlock", true);
				caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, platformVersion);
				caps.setCapability(MobileCapabilityType.UDID, deviceName);
				caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, loadPropFile.getProperty("appium.appPackage"));
				caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, loadPropFile.getProperty("appium.automationName"));
				caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, loadPropFile.getProperty("appium.appActivity"));	
				caps.setCapability(MobileCapabilityType.NO_RESET, loadPropFile.getProperty("appium.dontStopAppOnReset"));	
				caps.setCapability("waitForIdleTimeout", 500);
				caps.setCapability("waitForSelectorTimeout", 500);
				
				log.info("Starting Appium driver...");
				
				//Start Appium driver
				
					driver = new AppiumDriver<MobileElement>(new URL("http://127.0.0.1:" + portNumber + "/wd/hub"), caps);
				
				log.info("Appium driver started...");
				cpApp = new ClinicianProgrammer();
				tablet = new TabletIO();
		} catch (WebDriverException e) {			
			try {
				log.info("Retrying Appium driver start...");
				driver = new AppiumDriver<MobileElement>(new URL("http://127.0.0.1:" + portNumber + "/wd/hub"), caps);
			}catch(WebDriverException we) {
				log.error("Error starting Appium driver..." + we);
				we.printStackTrace();
				throw new WebDriverException();
			}
		} catch (MalformedURLException e) {		
			log.error("Error starting Appium driver..." + e.getMessage());
			log.error("Error starting Appium service...: full stack trace follows:", e);
			e.printStackTrace();
			throw new MalformedURLException();
		}
	}
	

	public void startAppiumService(String portNumber, String className) throws Exception {
		log.info("Starting Appium service. \n Logs will be stored at: " + appium_logfile_folder + "\\appium_"+ className + ".log");
		File appium_logfile = new File(appium_logfile_folder + "\\appium_"+ className + ".log");
		
		try {
				AppiumServiceBuilder builder = new AppiumServiceBuilder();
				builder.withIPAddress("127.0.0.1");
				builder.usingPort(Integer.parseInt(portNumber));
				builder.withLogFile(appium_logfile);
				//builder.withArgument(GeneralServerFlag.LOG_LEVEL, LogLevel.ERR.toString());
				service = AppiumDriverLocalService.buildService(builder);
			service.start();
				service.clearOutPutStreams();
				log.info("Service has been started with port number " + portNumber);
			}catch(Exception e) {
				log.error("Error starting Appium service...");
				try {
					log.info("Retrying Appium service start");
					//
					service.start();		
					service.clearOutPutStreams();
				}catch(Exception ae) {
					log.error("Error starting Appium service...: full stack trace follows:", ae);
					throw new Exception();
			}
		}
	}
	
	public void stopAppiumService() {

		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec("taskkill /F /IM node.exe");
			runtime.exec("taskkill /F /IM cmd.exe");
			log.info("Appium service stopped...");
		} catch (IOException e) {
			log.error("Could not stop Appium service...");
			e.printStackTrace();
		}
	}
	
	public static boolean checkIfServiceIsRunning(int port) {
		boolean isServerRunning = false;
		ServerSocket serverSocket;
		try {
			serverSocket = new ServerSocket(port);
			serverSocket.close();
		}catch(IOException e) {
			//If control comes here, then it means that the port is in use
			isServerRunning = true;
		}finally {
			serverSocket = null;
		}
		if(isServerRunning)
			System.out.println("Appium service is already running on port: " + port);
		return isServerRunning;
	}

My pom.xml has the following:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <selenium-version>3.141.59</selenium-version>
        <testng-version>7.5</testng-version>
        <appium-version>6.1.0</appium-version>
        <log4j-version>2.17.0</log4j-version>
        <extentreports-version>5.0.9</extentreports-version>
        <maven-surefire-plugin-version>3.0.0-M5</maven-surefire-plugin-version>
        <maven-jar-plugin-version>3.2.2</maven-jar-plugin-version>
       
    </properties>
    
      <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
		<dependency>
		    <groupId>io.appium</groupId>
		    <artifactId>java-client</artifactId>
		    <version>${appium-version}</version>
		</dependency>        

Could someone please help with the issue? Thank you!

Step 1: move appium server start to ‘beforeSuite’
Step 2: move driver start to ‘beforeMethod’
Step 3: move driver close to ‘afterMethod’
Step 4: move appium server close to ‘afterSuite’
Step 0: remove classname from start appium service.