Skip to content

[Question]Upload file got NullPointerException #1111

@Littlearphone

Description

@Littlearphone

I create an example project to test file upload.
when I call setInputFiles method, I got this exception:

Exception in thread "main" java.lang.NullPointerException: Cannot read field "isRemote" because the return value of "com.microsoft.playwright.impl.BrowserContextImpl.browser()" is null
	at com.microsoft.playwright.impl.Utils.addLargeFileUploadParams(Utils.java:168)
	at com.microsoft.playwright.impl.FrameImpl.setInputFilesImpl(FrameImpl.java:756)
	at com.microsoft.playwright.impl.FrameImpl.lambda$setInputFiles$36(FrameImpl.java:747)
	at com.microsoft.playwright.impl.LoggingSupport.lambda$withLogging$0(LoggingSupport.java:36)
	at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
	at com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:87)
	at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:35)
	at com.microsoft.playwright.impl.FrameImpl.setInputFiles(FrameImpl.java:747)
	at com.microsoft.playwright.impl.FrameImpl.setInputFiles(FrameImpl.java:733)
	at com.microsoft.playwright.impl.LocatorImpl.setInputFiles(LocatorImpl.java:452)
	at com.microsoft.playwright.Locator.setInputFiles(Locator.java:3282)
	at Example.main(Example.java:47)

I search over google, but not found any solutions except this post contains a similar description:
microsoft/playwright-python#1530 (comment)

My code is as follows:

import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType.LaunchPersistentContextOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Page.NavigateOptions;
import com.microsoft.playwright.Page.WaitForSelectorOptions;
import com.microsoft.playwright.Playwright;

import java.nio.file.Path;

import static com.microsoft.playwright.Playwright.create;
import static com.microsoft.playwright.options.WaitForSelectorState.ATTACHED;
import static com.microsoft.playwright.options.WaitForSelectorState.VISIBLE;
import static com.microsoft.playwright.options.WaitUntilState.DOMCONTENTLOADED;
import static java.nio.file.Paths.get;
import static java.util.Collections.singletonList;

public class Example {
    private static final String URL = "http://localhost/test-file-upload";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "test";
    
    public static void main(String[] args) {
        final Path data = get("D:\\workspace\\Complex\\playwright\\target");
        final Path path = get("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        final Path file = get("D:\\large-file.zip");
        try (
            Playwright playwright = create();
            BrowserContext context = playwright.chromium().launchPersistentContext(data, localExecutor(path));
        ) {
            Page page = context.newPage();
            page.navigate(URL, untilDocumentLoaded());
            System.out.println(page.title());
            page.waitForSelector("input[name=userId]", visible()).fill(USERNAME);
            page.waitForSelector("input[name=password]", visible()).fill(PASSWORD);
            page.waitForSelector("div.login-button", visible()).click();
            page.waitForSelector("button.upload-button", visible()).hover();
            page.locator("input[type=file]").setInputFiles(file);
            page.waitForTimeout(30000);
        }
    }
    
    private static WaitForSelectorOptions visible() {
        return new WaitForSelectorOptions().setState(VISIBLE);
    }
    
    private static WaitForSelectorOptions attached() {
        return new WaitForSelectorOptions().setState(ATTACHED);
    }
    
    private static NavigateOptions untilDocumentLoaded() {
        return new NavigateOptions().setWaitUntil(DOMCONTENTLOADED);
    }
    
    private static LaunchPersistentContextOptions localExecutor(Path path) {
        return new LaunchPersistentContextOptions()
            .setArgs(singletonList("--start-maximized"))
            .setIgnoreHTTPSErrors(true)
            .setExecutablePath(path)
            .setViewportSize(null)
            .setHeadless(false);
    }
}

My playwright version is:

    <dependencies>
        <dependency>
            <groupId>com.microsoft.playwright</groupId>
            <artifactId>playwright</artifactId>
            <version>1.27.1</version>
        </dependency>
    </dependencies>

And my chrome version is: 107.0.5304.88

Anyone can give me some advice? thanks~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions